Untitled
unknown
plain_text
3 years ago
1.1 kB
4
Indexable
#include <stdlib.h> #include <stdio.h> #include <sys/wait.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #include <unistd.h> int i; int main(){ int id,tmp; int *buf; id = shmget(IPC_PRIVATE,SHMLBA, IPC_CREAT|0600); if (id == -1){ perror("Utworzenie segmentu pamieci wspoldzielonej"); exit(1); } buf = shmat(id, NULL, 0); if (buf == NULL){ perror("Przylaczenie segmentu pamieci wspoldzielonej"); exit(1); } buf[0]=0; tmp=buf[0]; //proces 1 if(fork() == 0) { while(tmp!=0) tmp=buf[0]; buf[1]=1; buf[0]++; printf("Proces1: buf[1]=%d (buf[0]=%d)\n",buf[1],buf[0]); exit(0); } else { //proces 2 if(fork() == 0) { while(tmp!=1) tmp=buf[0]; buf[2]=2; buf[0]++; printf("Proces2: buf[2]=%d (buf[0]=%d)\n",buf[2],buf[0]); exit(0); } else wait(NULL); //proces 3 if(fork() == 0) { while(tmp!=2) tmp=buf[0]; buf[3]=3; buf[0]++; printf("Proces2: buf[3]=%d (buf[0]=%d)\n",buf[3],buf[0]); exit(0); } else wait(NULL); wait(NULL); for (i=0; i<4; i++){ printf("buf[%d]=%d\n", i, buf[i]); } shmctl(id,IPC_RMID,NULL); } return(0); }
Editor is loading...