创建一个共享内存
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
int shmid = shmget(IPC_PRIVATE, 1024, 0666);
if (shmid < 0)
{
printf("error\n");
} else
{
printf("succes\n");
}
shmat(shmid, NULL, )
return EXIT_SUCCESS;
}
然后使用这个共享内存
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/shm.h>
int main(int arg, char *args[])
{
char *shmbuf;
int shmid = 0;
if (arg > 2)
{
shmid = atoi(args[1]);
shmbuf = shmat(shmid, 0, 0);
if (atoi(args[2]) == 1)
{
scanf("%s", shmbuf);
}
if (atoi(args[2]) == 2)
{
printf("%s\n", shmbuf);
}
shmdt(shmbuf);
}
return 0;
}