帮忙解决两个LINUX的问题

来源:百度知道 编辑:UC知道 时间:2024/05/26 20:24:54
1.用fork()写一个简单的测试程序,从父进程和子进程中打印信息.信息应该包括父,子进程的PID.执行该进程若干次,看两个信息是否以同样的次序打印.
2.把wait()和exit()系统调用加到上一个上一个练习中,使子进程将退出状态返回给父进程,并将它包含在父进程的打印信息中.执行若干次,观察结果.

这是问题一:
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

int main()
{
pid_t pid;
pid = fork();
if(pid == 0)
{
int id = getpid();
printf("child process's pid is:%d\n", id);
}
if(pid > 0)
{
int id = getpid();
printf("father process's pid is:%d\n", id);
}
return 0;
}

这是问题二:
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
pid_t pid;
pid = fork();
if(pid == 0)
{
int id = getpid();
printf("child process's pid is:%d\n", id);
}

if(pid > 0)
{
int id = getpid();
printf("father process's pid