史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   請問一題c的fork在unix上執行 (http://forum.slime.com.tw/thread190607.html)

saltv2 2006-11-01 02:33 PM

請問一題c的fork在unix上執行
 
請教一下,我們的os要寫一題程式,但我們不常在用c的行程,所以一頭務水,我們大致上是寫說,要由二個行程,一個會做生產者執行Producer而另一個行程執行Customer的函數,但是我執行不出來,函數那些都沒問題,就差在main裡的行程我不會叫,
總而言之就是有一個行程執行Producer()
另一個執行Customer..
希望各位先進能幫我解決這一個問題
謝謝


#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
int Queue[11];
int Counter;
void Clear_Queue(){
int i;
for( i = 0;i<11;i++){
Queue[i]=0;
}
}
int random_at_range ( int lower, int upper)
{
return rand ( ) % (upper-lower+1) + lower;
}
void Producer(){
int i;
Counter=random_at_range(1,10);
Clear_Queue();
for(i=0;i<=Counter;i++){
Queue[i] = random_at_range(1,100);
}

}
void Customer(){
int i;
if(Counter==0){
printf("Queue is null\n");
return;
}
for(i=0;i<=Counter;i++){
printf("Queue[%d]=%d\n",i,Queue[i]);
}
}
int main()
{
srand ( (unsigned) time ( NULL ) );
int pid;
pid = fork();
if(pid == 0){
while(1){
Producer();
printf("excute Producer\n");
sleep(1);
}
exit(1);
}
else if(pid >0){
while(1){
Customer();
printf("excute customer\n");
sleep(1);
}
}
}

kinco 2006-11-02 03:17 AM

這要在LINUX上執行喔...

引用:

關於 fork():
#include <stdio.h>

void main(void)
{
int pid,i;

pid = fork();
if (pid == 0) {
for (i=0;i<44;i++) {
printf(" I am a child process I say %d\n",i);
sleep(1);
}
exit(1);
}
else if (pid > 0) {
for (i=0;i<40;i++) {
printf(" I am a parent process I say %d\n",i);
sleep(1);
}
}
else if (pid < 0)
printf(" Sorry .....I can't fork my self\n");
}

解釋一下 fork() 是怎麼動作的:
原來的 process
+---------+
| Data |
+---------+
| Code |---> PC
+---------+

執行 fork() 之後

原來的 process fork 出來的 process
+----------+ 複製 Data Code 到 +----------+
| Data | ===================> | Data |
+----------+ +----------+
| Code |---> CP = CP+1; | Code |----> CP = CP+1
+----------+ +----------+
由上圖可以看出原來的 process 和 fork 出來的 process 不論是變數、程式碼都是一模一樣的,唯一不同的是 fork() 的返回值,parent process 的返回值是 child process 的 PID ,而 child process 的返回值是 0 ,因此在程式實作上就以 fork() 的返回值來判斷那一個是 parent 那一個是 child 。


saltv2 2006-11-02 04:38 PM

因此,請問大大,
如果我要在main裡面寫二個行程...
一個呼叫Producer()的 function
另一個呼叫Customer()的function
我該怎麼做呢
在main裡面我該怎麼寫?

kinco 2006-11-03 09:02 AM

你可以用它fork()之後的pid值來判斷
你要宣告一個pid_t結構的變數
接下來就可以使用pid的數值去判斷了

你原來是宣告int pid 因為pid是一個結構化的東西
所以沒辦法用int 去接 一定要使用它的結構形態才可以使用

引用:

#include<sys/types.h>
#include<sys/wait.h>

pid_t wait(int* stat_loc)

fork( )有兩個 return 值,大於 0 的是 parent (值不固定),等於 0 的是 child,兩個 process 在 fork( ) 成功後 **同時存在**,並沒有什麼不一樣,可以說是完全一樣的process,fork( ) 之後的parent 與 child 會共同指向 fork() 以下的執行指令,也就是說,parent 和 child 都會執行 fork( ) 以下的程式碼,而且是各自執行,child 會符合 switch 中 pid == 0 的條件、 parent 則會符合 default 中的條件.
可以使用switch來檢驗你有沒有fork()成功
引用:

switch(pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
Cmessage = "This is the child,";
.
.
break;
default:
Cmessage = "This is the parent";
.
.
break;
}

之後再使用if判斷句 就可以達到你要的效果~


所有時間均為台北時間。現在的時間是 09:35 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2025, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1