㈠ 怎麼使用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!
附:我承認我是復制的 不過很符合題意~