#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
int main(int argc, const char *argv[])
{
//
key_t key = ftok("./",1);
if(key<1)
{
perror("ftok:");
return -1;
}
//
int shmid = shmget(key,128,IPC_CREAT|0664);
if(shmid < 0)
{
perror("shmid:");
return -1;
}
//
int* str = shmat(shmid,NULL,0);
if(str == (int*)-1)
{
perror("shmat:");
return -1;
}
//
int arr[] = {0,1,2,3,4,5,6,7};
int *p = str;
int len = sizeof(arr)/sizeof(arr[0]);
for(int i = 0;i<len;i++)
{
p[i] = arr[i];
}
//
pid_t cpid = fork();
if(cpid>0)
{
while(1)
{
if(p[0]==0)
{
for(int i =1;i<len;i++)
{
printf("%d",*(p+i));
}
putchar(10);
p[0] = 1;
}
}
}
else if(0 == cpid)
{
while(1)
{
if(p[0] == 1)
{
int temp = 0;
for(int i = 1,j = len-1;i<j;i++,j--)
{
temp = p[i];
p[i]=p[j];
p[j]=temp;
}
p[0] = 0;
}
}
}
else if(cpid<0)
{
perror("fork:");
return -1;
}
return 0;
}
在共享进程中放入一个字符串,创建两个进程一个进程倒置另一个进程打印要求打印的结果有序进程a:打印字符串b:逆置字符串、
于 2023-05-13 16:07:53 首次发布