Skip to content

Instantly share code, notes, and snippets.

@kotaroito
Created February 20, 2014 14:54
Show Gist options
  • Select an option

  • Save kotaroito/9115452 to your computer and use it in GitHub Desktop.

Select an option

Save kotaroito/9115452 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/shm.h>
#include "error.h"
int main(void)
{
int pid, shmid;
int *ip;
if ((shmid = shmget(IPC_PRIVATE, sizeof(int), 0600)) < 0 )
err_sys("shmget");
if ((ip = shmat(shmid, 0, 0)) == (void *) -1) {
err_sys("shmat");
}
*ip = 1;
if ((pid = fork()) < 0) {
err_sys("fork");
}
else if ( pid == 0 ) {
*ip = 100;
exit(EXIT_SUCCESS);
}
waitpid(pid, NULL, 0);
if (shmctl(shmid, IPC_RMID, 0) < 0)
err_sys("shmctl");
printf("*ip=%d\n", *ip); // 100
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment