Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <sys/stat.h>
- #include <sys/sem.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- struct sembuf p = {0,-1,SEM_UNDO};
- struct sembuf v = {0,1,SEM_UNDO};
- struct sembuf wait = {0,0,SEM_UNDO};
- struct sembuf p1 = {1,-1,SEM_UNDO};
- struct sembuf v1 = {1,1,SEM_UNDO};
- struct sembuf wait1 = {1,0,SEM_UNDO};
- struct sembuf p2 = {2,-1,SEM_UNDO};
- struct sembuf v2 = {2,1,SEM_UNDO};
- struct sembuf wait2 = {2,0,SEM_UNDO};
- union semun
- {
- int val;
- struct semid_ds *buf;
- ushort *array;
- } sem;
- struct sembuf sop;
- int semdec(int sid, int n)
- {
- sop.sem_num = n;
- sop.sem_op = -1;
- semop(sid, &sop, 1);
- }
- int seminc(int sid, int n)
- {
- sop.sem_num = n;
- sop.sem_op = 1;
- semop(sid, &sop, 1);
- }
- int waits(int sid, int n)
- {
- sop.sem_num = n;
- sop.sem_op = 0;
- semop(sid, &sop, 1);
- }
- void sleepbw(int ms)
- {
- usleep(ms*1000);
- }
- int main()
- {
- key_t key = 11;
- int semid = semget (key,1,0777|IPC_CREAT);
- printf("Semaphore created (%d)\n",semid);
- if (semid < 0)
- {
- exit(0);
- }
- int size = 2 * sizeof(int);
- int iterations = 3;
- sem.array = (ushort*)malloc(sizeof(ushort)*3);
- sem.array[0] = 1;
- sem.array[1] = 1;
- sem.array[2] = 1;
- sop.sem_flg = SEM_UNDO;
- semctl(semid, 3, SETALL, sem);
- semdec(semid,0);
- int pid = fork();
- if (pid != 0)
- {
- printf("Process 1 (%d)\n",getpid());
- int shmid = shmget(key,size, 0777| IPC_CREAT);
- char *addr = shmat(shmid,0,0);
- printf("Process 1 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
- int pid1 = fork();
- if (pid1!=0)
- {
- for (int i = 0; i < iterations; ++i)
- {
- sleepbw(3);
- printf("Process 1 waiting\n");
- waits(semid,0);
- seminc(semid, 0);
- strcat(addr, "1 ");
- strcat(addr, "1 ");
- printf("Process 1 executed\n");
- semdec(semid, 0);
- sleepbw(15);
- }
- shmdt(addr);
- }
- else
- {
- printf("Process 3 (%d)\n",getpid());
- int shmid = shmget(key,size,0);
- char *addr = shmat (shmid, 0,0);
- printf("Process 3 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
- for (int i = 0; i < iterations; ++i)
- {
- sleepbw(6);
- printf("Process 3 waiting\n");
- waits(semid,0);
- seminc(semid, 0);
- strcat(addr, "3 ");
- printf("Process 3 executed\n");
- semdec(semid, 0);
- sleepbw(15);
- }
- char buff[512];
- strcpy(buff,addr);
- //int file = open("text.txt",O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
- //printf("Process 3 (%d) OPEN FILE\n",getpid());
- //int check = write(file, buff, 2 * size);
- //if(check == 2 * size)
- printf("Process 3 (%d) has wrote %s\n",getpid(),buff);
- shmdt(addr);
- shmctl(shmid,IPC_RMID, NULL);
- exit(0);
- }
- }
- else
- {
- printf("Process 2 (%d) \n",getpid());
- int shmid = shmget(key,size,0);
- char *addr = shmat (shmid, 0,0);
- printf("Process 2 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
- for (int i = 0; i < iterations; ++i)
- {
- printf("Process 2 waiting\n");
- waits(semid,0);
- seminc(semid, 0);
- strcat(addr, "2 ");
- printf("Process 2 executed\n");
- semdec(semid, 0);
- sleepbw(15);
- }
- shmdt(addr);
- exit(0);
- }
- /*
- if (pid == 0)
- {
- printf("Process 2 (%d) START\n",getpid());
- int shmid = shmget(key,size,0);
- char *addr = shmat (shmid, 0,0);
- printf("Process 2 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
- for(int i = 0; i<interations; i++)
- {
- semop(semid, &wait,1);
- semop(semid, &v,1);
- printf("Process 2 (%d) blocked semaphore\n",getpid());
- strcat(addr, "2 ");
- semop(semid, &p,1);
- printf("Process 2 (%d) released semaphore\n", getpid());
- usleep(200000);
- }
- shmdt(addr);
- } else {
- int shmid = shmget(key,size, 0777| IPC_CREAT);
- char *addr = shmat(shmid,0,0);
- printf("Process 1 (%d) attached shared memory (shmid = %d,size = %d)\n",getpid(),shmid,size);
- for(int i = 0; i < interations; i++)
- {
- usleep(100000);
- semop(semid, &wait,1);
- semop(semid, &v,1);
- printf("Process 1 (%d) blocked semaPHore\n",getpid());
- strcat(addr,"1 ");
- strcat(addr,"1 ");
- semop(semid, &p, 1);
- printf("Process 1 (%d) released semaphore\n", getpid());
- usleep(100000);
- }
- char buff[128];
- strcpy(buff,addr);
- shmdt(addr);
- shmctl(shmid,IPC_RMID, NULL);
- printf("Process 2 (%d) deleted shared memory\n",getpid());
- if(fork() == 0)
- {
- printf("Process 3 (%d) START\n",getpid());
- int file = open("text.txt",O_RDWR | O_TRUNC | O_CREAT, S_IREAD | S_IWRITE);
- printf("Process 3(%d) OPEN FILE\n",getpid());
- int check = write(file, buff, 2 * size);
- if(check == 2 * size) printf("Process 3 (%d) has wrote %s\n",getpid(),buff);
- }
- exit(0);
- }
- */
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement