Tag: 分叉

叉子(); C中的方法:确定顺序

我试图理解叉子在叉子上是如何工作的,但我在某处误解了某些东西。 去年我的教授给了我一个测试,但是我无法回复它:我们有3个任务(进程或线程),使用以下伪代码: Th1 { display “Hello 1” } Th2 { display “Hello 2” } Th3 { display “Hello 3” } main() { Fork(Th1);Fork(Th2);Fork(Th3); } 问题是: Which is the order of the execution of these tasks? Why? Which is the order of the execution of these tasks? Why? 我怎么回复这个? 是否有任何指南或任何有用的资源我可以理解叉子,信号量和内存分配? 我是低级编程的真正新手。

全局变量不保留其在线程中的值

我创建了一个multithreading服务器,它有一个指向链表的全局指针,在线程中我试图插入一些数据,但是那些数据(我插入的)没有保留,可能是在全局值没有保留的线程中。 我使用以下代码(这是最简单的版本。) struct node { int cn; // struct node *next; }; /*GLOBAL VARIABLES*/ struct node *start; //Global pointer to Linked List /* END */ int main(int argc, char *argv[]) { start = (struct node *)malloc(sizeof(struct node)); start -> cn =0; int pid; /* Declaration of Server goes here */ printf(“Server Running …\n”); while (1) […]

为什么在使用fork系统调用时打印了很多PID?

我正在学习Linux中的进程管理,我尝试实现以下C程序,其输出打印了15个PID(4个唯一的PID)。 我试图找出进程族树,但它确实没有帮助我理解为什么PID被打印了这么多次。 我浏览了几个链接,包括http://u.cs.biu.ac.il/~linraz/os/OS2.pdf,http://www.ibm.com/developerworks/aix/library/au-unixprocess.html , 谁在fork()之后首先执行:父或孩子? 。 但是,我找不到解决方案。 如果有人帮助我理解这个问题,那将会很有帮助。 #include #include #include int main() { printf ( “Parent:%d Child: %d\n”,getppid(),getpid()); // To print the PIDs of the parent process and the child process fork(); //System call to spawn a child process printf ( “Parent:%d Child: %d\n”,getppid(),getpid()); fork(); printf ( “Parent:%d Child: %d\n”,getppid(),getpid()); fork(); printf ( “Parent:%d […]

在C中调用fork()之后引用指针

所以,我有一个函数,用一些字符串数据加载一个char **变量。 我的目标是分叉进程,并在子进程中打印一些数据,并从父进程中打印一些数据。 但是,我在fork()调用后无法引用指针。 我认为fork()制作了父进程的整个地址空间的副本,似乎它将包含各种堆栈指针…… 基本上,我的代码目前看起来像这样: load_data(char **data); char** data; load_data(data); printf(“String 0: %s\n”, data[0]); fork(); printf(“String 0 again: %s\n”, data[0]); /* Segfaults Here! */ 任何人都有任何想法,我做错了什么? 我已经做了一些谷歌搜索,这似乎我正在做的应该工作 – 但它没有。 因此,我误解了一些基本的东西……

多次并行调用函数

我想多次调用具有不同arg值的函数。 为了使执行更快,我想调用函数而不考虑它的实现。 在循环内部我将arg传递给函数并调用它现在我的程序不应该等待函数执行并再次循环,而是它不应该认为函数没有完成第一个循环它应该继续并调用一次又一次地起作用直到循环结束。 如何使用fork和threads来做到这一点,哪一个更快,一个代码框架会有帮助? 让我说我的函数是void foo(arg1,arg2)

fork调用后的地址空间

当进程执行fork()sys调用时,会从中生成子进程。 fork()调用之后的所有代码都被复制到内存的新物理页面,即帧。 我无法可视化子进程的虚拟内存部分。因为在下面的代码中,char变量的地址在child和parent中是相同的。 #include #include int main(void) { pid_t pid; char y=’Y’; char *ptr; ptr=&y; pid = fork(); if (pid == 0) { y=’Z’; printf(” *** Child process ***\n”); printf(” Address is %p\n”,ptr); printf(” char value is %c\n”,y); sleep(5); } else { sleep(5); printf(“\n ***parent process ***\n”,&y); printf(” Address is %p\n”,ptr); printf(” char value is […]

fork()系统调用和进程的内存空间

我引用“当进程使用fork()调用创建一个新进程时,只有父进程和新分叉的子进程之间共享共享内存段。堆栈和堆的副本是为新创建的进程”从Silberschatz的“操作系统概念”解决方案。 但是当我尝试这个程序的时候 #include #include #define MAX_COUNT 200 void ChildProcess(void); /* child process prototype */ void ParentProcess(void); /* parent process prototype */ void main(void) { pid_t pid; char * x=(char *)malloc(10); pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); printf(“the address is %p\n”,x); } void ChildProcess(void) { printf(” *** Child process ***\n”); } void […]

使用这些fork()语句创建了多少个进程?

我相信这创造了24个过程; 但是,我需要validation。 这些问题经常让我感到困惑。 谢谢您的帮助! #include #include #include int main(void) { pid_t pid = fork(); pid = fork(); pid = fork(); if (pid == 0) { fork(); } fork(); return 0; }

为什么rand()在fork之后不是那么随机?

#include #include #include #include int main() { int i =10; /* initialize random seed: */ srand(time(NULL)); while(i–){ if(fork()==0){ /* initialize random seed here does not make a difference: srand(time(NULL)); */ printf(“%d : %d\n”,i,rand()); return; } } return (EXIT_SUCCESS); } 打印相同(每次运行时不同)数量10次 – 预计? 我有一个更复杂的代码片段,每个分叉的进程依次运行 – 没有区别