㈠ 怎么使用fork函数创建子进程的子进程
//main1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
printf("program 1 started\n");
char *newargv[] = { "hello", "world" };
char *newenviron[] = { NULL };
int pid=fork();
if(pid < 0)
exit(EXIT_FAILURE);
if(pid == 0)
{
execve("./main2", newargv, newenviron); //打开同级目录下的main2程序
perror("execve"); /* execve() only returns on error */
exit(EXIT_FAILURE);
}
//父进程继续
sleep(5); //do something
printf("Promgram 1 finished!\n");
return 0;
}
//main2.c
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
printf("program 2 started\n");
int i;
for(i = 0; i<argc;i++)
printf("%s\n",argv[i]);
sleep(5); //do something
printf("Promgram 2 finished!\n");
return 0;
}
然后编译
gcc -o main1 main1.c
gcc -o main2 main2.c
运行
./main1
㈡ 编写一C语言程序,实现在程序运行时通过系统调用fork( )创建两个子进程
#include <stdio.h>
int main()
{
int pid;
/*这里创建了一个子进程1*/
pid=fork();
if(pid==0)
printf("I am son,my pid is %d.\n",getpid());
else if(pid>0)
{
/*从子进程1返回到父进程时,再创建子进程2。*/
printf("I'm father ,my pid is %d.\n",getpid());
pid=fork();
if(pid==0)
printf("I'm daughter process,my pid is %d.\n",getpid());
else if(pid>0)
printf("I'm father process,my pid is %d.\n",getpid());
else
printf("fork() error.\n");
}
else printf("fork() error.\n");
}
㈢ 编写一C语言程序,实现在程序运行时通过系统调用fork( )创建两个子进程
#include
<stdio.h>
int
main()
{
int
pid;
/*这里创建了一个子进程1*/
pid=fork();
if(pid==0)
printf("I
am
son,my
pid
is
%d.
",getpid());
else
if(pid>0)
{
/*从子进程1返回到父进程时,再创建子进程2。*/
printf("I'm
father
,my
pid
is
%d.
",getpid());
pid=fork();
if(pid==0)
printf("I'm
daughter
process,my
pid
is
%d.
",getpid());
else
if(pid>0)
printf("I'm
father
process,my
pid
is
%d.
",getpid());
else
printf("fork()
error.
");
}
else
printf("fork()
error.
");
}
㈣ C语言创建进程
fork()应该是linux操作系统的函数。
从#include <sys/signal.h>和fork()上判断,这个程序应该是Linux操作系统的应用程序。
Windows上应该用CreateProcess创建进程
获取进程ID用
DWORD GetWindowThreadProcessld(HWND hwnd,LPDWORD lpdwProcessld)
hWnd:窗口句柄。
lpdwProcessld:接收进程标识的32位值的地址。如果这个参数不为NULL,GetWindwThreadProcessld将进程标识拷贝到这个32位值中,否则不拷贝。
返回值:返回值为创建窗口的线程标识。
㈤ fork函数是什么
C语言里面,fork函数是用来创建子进程的
㈥ 编写程序,使用fork( )创建两个子进程。观察在程序运行过程中的进程状态变化,分析原因。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int p1,p2;
if(p1=fork())
{
printf("I am child 1。\n");
fork();
}
else
{
if(p2=fork()) printf("I am child 2。\n");
else printf("I am parent。\n");
}
return 0;
}
㈦ c语言中创建子进程运行外部程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid1, pid2, pid3, pid4, pid5, pid6;
pid1 = getpid();
printf("PID = %d\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
pid3 = fork();
if (pid3 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid5 = fork();
if (pid5 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid6 = fork();
if (pid6 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
pid4 = fork();
if (pid4 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
return 0;
}
每一个进程都会在退出前 sleep 30秒,从而保证能够用 ps 看到,
编译 gcc testpid.c -o testpid
然后执行,可以看到
PID = 24913
PID = 24914, Parent PID = 24913
PID = 24916, Parent PID = 24913
PID = 24915, Parent PID = 24913
PID = 24917, Parent PID = 24915
PID = 24918, Parent PID = 24917
ps -ef 的结果
24913 24582 0 11:29 pts/19 00:00:00 ./testpid
24914 24913 0 11:29 pts/19 00:00:00 ./testpid
24915 24913 0 11:29 pts/19 00:00:00 ./testpid
24916 24913 0 11:29 pts/19 00:00:00 ./testpid
24917 24915 0 11:29 pts/19 00:00:00 ./testpid
24918 24917 0 11:29 pts/19 00:00:00 ./testpid
㈧ linux下用fork(),execve()函数创建子进程,并用子进程执行另外一个程序
//main1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
printf("program 1 started\n");
char *newargv[] = { "hello", "world" };
char *newenviron[] = { NULL };
int pid=fork();
if(pid < 0)
exit(EXIT_FAILURE);
if(pid == 0)
{
execve("./main2", newargv, newenviron); //打开同级目录下的main2程序
perror("execve"); /* execve() only returns on error */
exit(EXIT_FAILURE);
}
//父进程继续
sleep(5); //do something
printf("Promgram 1 finished!\n");
return 0;
}
//main2.c
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
printf("program 2 started\n");
int i;
for(i = 0; i<argc;i++)
printf("%s\n",argv[i]);
sleep(5); //do something
printf("Promgram 2 finished!\n");
return 0;
}
然后编译
gcc -o main1 main1.c
gcc -o main2 main2.c
运行
./main1
㈨ 编写一段c语言程序,使进程1创建出2 3 4 子进程,子进程3创建孙进程5 ,5在创建出重孙6
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid1, pid2, pid3, pid4, pid5, pid6;
pid1 = getpid();
printf("PID = %d\n", pid1);
pid2 = fork();
if (pid2 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
pid3 = fork();
if (pid3 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid5 = fork();
if (pid5 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
pid6 = fork();
if (pid6 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
sleep(30);
exit(0);
}
pid4 = fork();
if (pid4 == 0)
{
printf("PID = %d, Parent PID = %d\n", getpid(), getppid());
sleep(30);
exit(0);
}
sleep(30);
return 0;
}
每一个进程都会在退出前 sleep 30秒,从而保证能够用 ps 看到,
编译 gcc testpid.c -o testpid
然后执行,可以看到
PID = 24913
PID = 24914, Parent PID = 24913
PID = 24916, Parent PID = 24913
PID = 24915, Parent PID = 24913
PID = 24917, Parent PID = 24915
PID = 24918, Parent PID = 24917
ps -ef 的结果
24913 24582 0 11:29 pts/19 00:00:00 ./testpid
24914 24913 0 11:29 pts/19 00:00:00 ./testpid
24915 24913 0 11:29 pts/19 00:00:00 ./testpid
24916 24913 0 11:29 pts/19 00:00:00 ./testpid
24917 24915 0 11:29 pts/19 00:00:00 ./testpid
24918 24917 0 11:29 pts/19 00:00:00 ./testpid
㈩ 编写一个程序实现以下功能: (1)使用fork()创建进程。 (2)使用管道实现子进程和父进程之间的通信。
编写一段程序,使用系统调用fork( )创建两个子进程。当此程序运行时,在系统中有一个父进程和两个子进程活动。让每一个进程在屏幕上显示一个字符;父进程显示字符“a”,子进程分别显示字符“b”和“c”。试观察记录屏幕上的显示结果,并分析原因。
〈程序〉
#include<stdio.h>
main()
{
int p1,p2;
if(p1=fork()) /*子进程创建成功*/
putchar('b');
else
{
if(p2=fork()) /*子进程创建成功*/
putchar('c');
else putchar('a'); /*父进程执行*/
}
}
<运行结果>
bca(有时会出现abc的任意的排列)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
编制一段程序,实现进程的管道通信。使用系统调用pipe()建立一条管道线。两个子进程p1和p2分别向通道个写一句话:
child1 process is sending message!
child2 process is sending message!
而父进程则从管道中读出来自两个进程的信息,显示在屏幕上。
〈程序〉
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int pid1,pid2;
main( )
{
int fd[2];
char outpipe[100],inpipe[100];
pipe(fd); /*创建一个管道*/
while ((pid1=fork( ))==-1);
if(pid1==0)
{
lockf(fd[1],1,0);
sprintf(outpipe,"child 1 process is sending message!");
/*把串放入数组outpipe中*/
write(fd[1],outpipe,50); /*向管道写长为50字节的串*/
sleep(5); /*自我阻塞5秒*/
lockf(fd[1],0,0);
exit(0);
}
else
{
while((pid2=fork( ))==-1);
if(pid2==0)
{
lockf(fd[1],1,0); /*互斥*/
sprintf(outpipe,"child 2 process is sending message!");
write(fd[1],outpipe,50);
sleep(5);
lockf(fd[1],0,0);
exit(0);
}
else
{
wait(0); /*同步*/
read(fd[0],inpipe,50); /*从管道中读长为50字节的串*/
printf("%s\n",inpipe);
wait(0);
read(fd[0],inpipe,50);
printf("%s\n",inpipe);
exit(0);
}
}
}
〈运行结果〉
延迟5秒后显示:
child1 process is sending message!
再延迟5秒:
child2 process is sending message!
附:我承认我是复制的 不过很符合题意~