[英]I wrote this code and need to know if there is synchronization problem in updating the variable value?
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 10;
int main()
{
pid_t pid;
pid = fork();
if (pid == 0)
{
value = value + 100;
}
else if (pid > 0)
{
value = value - 100;
printf("PARENT: value = %d \n", value); //Line A
wait(NULL);
}
}
我寫了這段代碼,需要知道更新變量值是否存在同步問題
當你叉,父母和孩子是兩個不同的過程, value
父母的value
孩子是兩個不同的情況下,改變其中一個不會影響其它,我們不能真的談論在這種情況下同步。
至於進程本身,存在一定程度的同步,因為父進程在繼續之前等待子進程。
您缺少#include <sys/wait.h>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.