Beruflich Dokumente
Kultur Dokumente
// //
Lesson#3
} return 0;
execvp:
#include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int i; int pid; for (i=0; i<argc; ++i) printf("argument[%d]: %s\n", i, argv[i]);
pid = fork(); if (0==pid) execvp("ls",argv); else { if (-1 == pid) perror("fork"); else { wait(NULL); printf("parent: pid: %d getpid(): %d getppid(): %d\n", pid, getpid(), getppid()); } }
return 0; }
int main() { int fd[2]; char buff[4096]; int rb; if ( pipe(fd) ) { perror("pipe"); exit(-1); } if (fork()) // father { printf("parent pid: %d\n", getpid()); write(fd[1], "Hello son!\n", sizeof("Hello son!\n")); wait(NULL); } else // son { rb = read(fd[0], buff, sizeof(buff)); printf("son pid: %d, parent pid: %d\n", getpid(), getppid()); write(1, buff, rb); } close(fd[0]); close(fd[1]); return 0; }
void cleanup(int); char str[]="shalom"; char *sharestr; int shmid; int main() { int s,i; shmid=shmget ( IPC_PRIVATE, strlen ( str ) + 1, 0600 ); printf("shmid: %d\n", shmid); sharestr=(char *)shmat ( shmid, 0 , 0); strcpy ( sharestr , str ); // copying "shalom" to sharestr before fork! printf ( "%s (%p)\n" , sharestr, sharestr); if ( fork()==0 ) // son { strcpy ( sharestr , "hello world!!!" ); // shared => visible to parent strcpy( str, "byebye"); // not shared => change local to son. printf ( "son:\n\tstr: %s\n\tsharestr: %s\n" , str, sharestr ); shmdt ( sharestr ); } else // father { wait ( &s ); printf ( "parent:\n\tstr: %s\n\tsharestr: %s\n" , str, sharestr ); shmdt ( sharestr ); shmctl ( shmid , IPC_RMID , 0 ); } return 0; }
semaphore.h
#ifndef __SEMAPHORE_H__ #define __SEMAPHORE_H__ void void void void init(int _nlocks); finilize(); lock(int locknum); unlock(int locknum);
#endif
semaphore.c
#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdio.h> #include <stdlib.h> #include "semaphore.h" union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; }; static int semid; static int nlocks; void err_exit(const char *str) { perror(str); exit(-1); } void init(int _nlocks) { int i; union semun sem_init; nlocks=_nlocks; semid = semget(IPC_PRIVATE, nlocks, 0600); if (semid < 0) err_exit("semget"); sem_init.array = (unsigned short *)malloc(sizeof(short)*nlocks); for (i=0; i<nlocks; ++i) sem_init.array[i] = 1; if ( semctl(semid, 0, SETALL, sem_init) < 0) { finilize(); err_exit("semctl"); } } void finilize() { if ( semctl(semid, 0, IPC_RMID) < 0) { finilize(); err_exit("semctl"); } /* /* /* /* Value for SETVAL */ Buffer for IPC_STAT, IPC_SET */ Array for GETALL, SETALL */ Buffer for IPC_INFO (Linux-specific) */
} void lock(int locknum) { struct sembuf sop; sop.sem_num = locknum; sop.sem_op = -1; sop.sem_flg = 0; if (semop(semid, &sop, 1) < 0) { finilize(); err_exit("semop"); } } void unlock(int locknum) { struct sembuf sop; sop.sem_num = locknum; sop.sem_op = 1; sop.sem_flg = 0; if (semop(semid, &sop, 1) < 0) { finilize(); err_exit("semop"); } }
sem_test.c
int main() { init(2); fork(); fork(); lock(0); lock(1); printf("locking\n"); sleep(1); printf("unlocking\n"); unlock(1); unlock(0); sleep(4); printf("clearing...\n"); finilize(); return 0; }
Casting: cast.c
int main() { int i = 66000, j; short short_arr[2]; int *ptr; short_arr[0]=464; short_arr[1]=1; printf("%d\n", *((int*)short_arr)); write(1, &i, 4); for (j=0; j<2; j++) printf("%d ", ((short*)&i)[j]); printf("\n"); printf("int: %d, short: %d\n", i, (short)i); ptr = &i; printf("char: %d\n", *((unsigned char*)ptr)); return 0; }
//
// // //
Threads samples
Threads with simple mutex: threads.c
#include <pthread.h> #include <stdio.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thread_main(void *arg) { printf("in thread %d\n", pthread_self()); pthread_mutex_lock(&mutex); printf("in thread %d locked\n", pthread_self()); sleep(1); pthread_mutex_unlock(&mutex); printf("in thread %d unlocked\n", pthread_self()); return NULL; } int main() { pthread_t tid[2]; pthread_create(tid, NULL, thread_main, NULL); pthread_create(tid+1, NULL, thread_main, NULL); pthread_exit(NULL); // // pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); printf("main finished\n"); return 0; }
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; static int count=0; void *thread_main(void *arg) { printf("in thread %d\n", pthread_self()); pthread_mutex_lock(&mutex); printf("Before cond %d\n", pthread_self()); sleep(1); if (!count) { ++count; pthread_cond_wait(&cond, &mutex); } else { pthread_cond_signal(&cond); } printf("After cond: %d\n", pthread_self()); pthread_mutex_unlock(&mutex); printf("in thread %d unlocked\n", pthread_self()); return NULL; } int main() { pthread_t tid[2]; pthread_create(tid, NULL, thread_main, NULL); pthread_create(tid+1, NULL, thread_main, NULL); pthread_exit(NULL); // // pthread_join(tid[0], NULL); pthread_join(tid[1], NULL); printf("main finished\n"); return 0; }