Navigationskarta Insitutitionen för Datavetenskap Umeå Universitet

Kodexempel F11

PostScript-dokumentet os-intro.ps innehåller en hel del bra material om dödlägen, utsvältning, synkronisering och liknande. (Överkurs.)





#include <sys/types.h>
#include <sys/stat.h>

int mkfifo(const char *pathname, mode_t mode);


total 160
drwx--l---   2 ath      tdb          512 Sep 28 17:34 ./
drwx--l---   4 ath      tdb          512 Sep 28 12:59 ../
-rw-------   1 ath      tdb           94 Sep 28 17:32 fifo.c
prw-------   1 ath      tdb            0 Sep 28 17:34 fifo_fil|
-rwx------   1 ath      tdb        24552 Sep 28 17:28 popen*
-rw-------   1 ath      tdb          235 Sep 28 17:29 popen.c
-rwx------   1 ath      tdb        24676 Sep 28 15:36 shmc*
-rw-------   1 ath      tdb          478 Sep 28 15:36 shmc.c
-rwx------   1 ath      tdb        24816 Sep 28 15:36 shms*
-rw-------   1 ath      tdb          602 Sep 28 15:35 shms.c


#include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semget(key_t key, int nsems, int semflg); int semctl(int semid, int semnum, int cmd, ...); int semop(int semid, struct sembuf *sops, size_t nsops); void init_sema(key_t semkey, int *semid) { union semun sema_zero; sema_zero.val=0; if ((*semid=semget(semkey, 1, 0600|IPC_CREAT|IPC_EXCL))==-1) ... /* fel */ if (semctl(*semid, 0, SETVAL, sema_zero)==-1) ... /* fel */ } /* wait på semafor */ struct sembuf p={0,-1,0}; /* Motsvarande för signal: 0,1,0 */ if (semop(sem, &p, 1)==-1) ... /* fel */ /* Ta bort semafor */ if (semctl(glob.sem, 1, IPC_RMID)==-1) ... /* fel */
#include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> int shmctl(int shmid, int cmd, struct shmid_ds *buf); int shmget(key_t key, size_t size, int shmflg); void *shmat(int shmid, const void *shmaddr, int shmflg); int shmdt(char *shmaddr); void init_shm(key_t shmkey, int *shmid, void **block) { if ((*shmid=shmget(shmkey, 8192, 0600|IPC_CREAT|IPC_EXCL))==-1) ... /* fel */ if ((*block=shmat(glob.shm, (void *) 0, 0))==(void *)-1) ... /* fel */ } /* Ta bort delat minne */ if (shmdt(block)==-1) ... /* fel */ if (shmctl(shmid, IPC_RMID, NULL)==-1) ... /* fel */
[an error occurred while processing this directive]