網誌
code
熱度 5
#include<stdio.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<sys/shm.h>
#include<signal.h>
#include<unistd.h>
void edt(int sig)
{
printf("child process finish!! \n");
}
int shmid=0;
int main(void)
{
//pid_t childpid;
//childpid=fork();
shmid=shmget(IPC_PRIVATE,sizeof(int),S_IRUSR|S_IWUSR);
int *spt1=(int*)shmat(shmid,NULL,0);
int *spt2=(int*)shmat(shmid,NULL,0);
pid_t childpid;
childpid=fork();
if(childpid==0)
{
sleep(1);
int a=51,b=0;
for(a;a<101;a++){b=a+b;}
if(a==101)
{
*spt1=*spt1+b;
printf("a=%d ,b= %d \n",a,*spt1);
kill(getppid(),SIGALRM);
}
}
if(childpid>0)
{
int a=1,b=0;
for(a;a<51;a++){b=a+b;}
printf("parent is %ld ,child is %ld \n",(long)getpid(),(long)childpid);
(void)signal(SIGALRM,edt);
wait(NULL);
*spt2=*spt1+b;
printf("b= %d \n",*spt2);
return 0;
}
}