目錄
前言
1 實(shí)驗(yàn)題目
2 實(shí)驗(yàn)?zāi)康?/p>
3 實(shí)驗(yàn)內(nèi)容
3.1 進(jìn)程的創(chuàng)建
3.1.1 步驟
3.1.2 關(guān)鍵代碼
3.2 子進(jìn)程執(zhí)行新任務(wù)
3.2.1 步驟
3.2.2?關(guān)鍵代碼
4 實(shí)驗(yàn)結(jié)果與分析
4.1 進(jìn)程的創(chuàng)建
4.2 子進(jìn)程執(zhí)行新任務(wù)
5 代碼
5.1 進(jìn)程的創(chuàng)建
5.2 子進(jìn)程執(zhí)行新任務(wù)
前言
?????????本實(shí)驗(yàn)為課設(shè)內(nèi)容,博客內(nèi)容為部分報(bào)告內(nèi)容,僅為大家提供參考,請勿直接抄襲,另外,本次實(shí)驗(yàn)所用平臺是Ubuntu 22.04 LTS,代碼均是在終端進(jìn)行編譯的,不會的可以先了解怎么用終端編程,或者利用其他較為智能的開發(fā)環(huán)境進(jìn)行編程
1 實(shí)驗(yàn)題目
????????實(shí)驗(yàn)二?Linux 進(jìn)程管理
2 實(shí)驗(yàn)?zāi)康?/h2>
????????通過進(jìn)程的創(chuàng)建、撤銷和運(yùn)行加深對進(jìn)程概念和進(jìn)程并發(fā)執(zhí)行的理解,明確進(jìn)程和程序之間的區(qū)別。???
3 實(shí)驗(yàn)內(nèi)容
3.1 進(jìn)程的創(chuàng)建
3.1.1 步驟
? ? ? ? (1)步驟1:在目錄實(shí)驗(yàn)2下用touch命令新建一個(gè)實(shí)驗(yàn)2-1.c文件,利用gedit命令打開編輯,復(fù)制清單3-1中的代碼到實(shí)驗(yàn)2-1.c文件中并保存,利用gcc命令將其編譯成可執(zhí)行文件實(shí)驗(yàn)2-1。
? ? ? ? (2)步驟2:在命令行輸入./實(shí)驗(yàn)2-1運(yùn)行該程序,觀察運(yùn)行結(jié)果。
? ? ? ? (3)步驟3:多次運(yùn)行程序,觀察運(yùn)行結(jié)果。
3.1.2 關(guān)鍵代碼
if (x==0)
{
sleep(rand() % 2);
printf("a\n");
}
else
{
sleep(rand() % 3);
printf("b\n");
}
if(x==0)printf("cc\n");
else printf("fc\n");
3.2 子進(jìn)程執(zhí)行新任務(wù)
3.2.1 步驟
? ? ? ? (1)步驟1:在目錄實(shí)驗(yàn)2下用touch命令新建一個(gè)實(shí)驗(yàn)2-2.c文件,利用gedit命令打開編輯,復(fù)制清單3-2中的代碼到實(shí)驗(yàn)2-2.c文件中并保存,利用gcc命令將其編譯成可執(zhí)行文件實(shí)驗(yàn)2-2。
? ? ? ? (2)步驟2:在命令行輸入./實(shí)驗(yàn)2-2運(yùn)行該程序,觀察運(yùn)行結(jié)果。
3.2.2?關(guān)鍵代碼
pid_t pid;
/*fork another process*/
pid = fork();
if(pid<0){
fprintf(stderr,"Fork Failed");
exit(-1);
}else if(pid==0){
execlp("/bin/ls","ls",NULL);
}else{/*parent process*/
/*parent wait for the child to complete*/
wait(NULL);
printf("Child Complete\n");
exit(0);
}
4 實(shí)驗(yàn)結(jié)果與分析
4.1 進(jìn)程的創(chuàng)建
執(zhí)行步驟2和3后,可以看到每次的結(jié)果不一定相同,可能是父進(jìn)程先執(zhí)行完畢,也可能是子進(jìn)程先執(zhí)行完畢,這說明進(jìn)程的調(diào)度是不可預(yù)測的,即程序的執(zhí)行不可在現(xiàn),結(jié)果如下圖所示:
圖1.1 實(shí)驗(yàn)2-1步驟2和3
4.2 子進(jìn)程執(zhí)行新任務(wù)
執(zhí)行步驟2后,會看到在終端上先輸出了目錄下的文件情況,再輸出”Child Compelete”,說明父進(jìn)程執(zhí)行wait之后,會等待子進(jìn)程執(zhí)行完才會繼續(xù)執(zhí)行,結(jié)果如下圖所示:
文章來源:http://www.zghlxwxcb.cn/news/detail-802669.html
圖1.2 實(shí)驗(yàn)2-2步驟2文章來源地址http://www.zghlxwxcb.cn/news/detail-802669.html
5 代碼
5.1 進(jìn)程的創(chuàng)建
#include<stdio.h>
#include<stdlib.h>
int main(void){
int x;
srand((unsigned)time(NULL));
while((x=fork())==-1);
if(x==0){
sleep(rand()%2);
printf("a\n");
}else{
sleep(rand()%3);
printf("b\n");
}
if(x==0)printf("cc\n");
else printf("fc\n");
}
5.2 子進(jìn)程執(zhí)行新任務(wù)
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main(void){
pid_t pid;
/*fork another process*/
pid = fork();
if(pid<0){
fprintf(stderr,"Fork Failed");
exit(-1);
}else if(pid==0){
execlp("/bin/ls","ls",NULL);
}else{/*parent process*/
/*parent wait for the child to complete*/
wait(NULL);
printf("Child Complete\n");
exit(0);
}
}
到了這里,關(guān)于操作系統(tǒng)課程設(shè)計(jì)-Linux 進(jìn)程控制的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!