Last active
March 5, 2018 17:08
-
-
Save maacpiash/a0e273b3f624fd36883c05ceae9510f5 to your computer and use it in GitHub Desktop.
NSU CSE 323 (IHa) Spring 2018
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/wait.h> | |
int main( void ) | |
{ | |
int shmId; | |
key_t key; | |
int *shmAddr, *s; | |
int i; | |
pid_t pid = getpid(); | |
key = 5678; | |
if ((shmId = shmget(key, sizeof(int), IPC_CREAT | 0666)) < 0) { | |
perror("shmget"); | |
exit(1); | |
} | |
if ((shmAddr = shmat(shmId, NULL, 0)) == (int *) -1) { | |
perror("shmat"); | |
exit(1); | |
} | |
*shmAddr = 0; | |
for(i=0; i<100; i++) | |
{ | |
if(fork() == 0) | |
{ | |
for(i=0; i<100; i++) | |
{ | |
printf("%d + 1 = ", *shmAddr); | |
*shmAddr = *shmAddr + 1; | |
printf("%d\n", *shmAddr); | |
} | |
} | |
else | |
{ | |
wait(NULL); | |
} | |
} | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment