Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Help With Fork()!
sonalurfrnd
post Aug 9 2007, 01:22 AM
Post #1


Newbie
*

Group: Members
Posts: 1
Joined: 9-August 07
Member No.: 47,818



help me with this: -

CODE
void main()
{
    int i=0;
    fork();
    printf("%d", i++);
    fork();
    printf("%d", i++)
    fork();
    wait();
}


please help mw with output of this program
Notice from Plenoptic:
Added code tags. Any code, or copied content not in your own words must be placed in the proper BBCode tags. Please review the Trap17 ReadMe for further rules and guidelines.
Go to the top of the page
 
+Quote Post
rajibbd
post Aug 9 2007, 07:47 PM
Post #2


Newbie [Level 3]
***

Group: Members
Posts: 47
Joined: 22-July 07
From: Dhaka, Bangladesh
Member No.: 46,859



Though i am not good at C/C++. But seeing this code what i can get that, the main function have a inter value of 'i' assigning to '0', after then there was a function called "fork()" my question is what did its do. then the printf function should print 1[because 'i' is increasing]. Then again and again printf function which should print 2 for same case and there is another fork. and a wait function[with out giving how far we should wait].
Go to the top of the page
 
+Quote Post
osknockout
post Aug 10 2007, 04:31 PM
Post #3


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



@rajibbd: fork() creates a process running the program. If you had two cpu's, you'd theoretically fork a program once to have parts of it running on both.

@sonalufrnd: Ok, what exactly are you trying to do? It looks like you're trying to get the result "1 1 2 2 2 2".
It'd help if you could tell us what the aim of the code is.
- and you're missing a semicolon on the second printf().

I'm under the impression that you need to get only the parent process to wait() otherwise all processes wil be waiting for other processes to finish that don't exist
- I haven't tested the code or played with processes for a while, so I'm not exactly sure. However, if that is the case, you need to be able to distinguish the parent process from the others.

This page should help a good bit.
Go to the top of the page
 
+Quote Post
rajibbd
post Aug 10 2007, 04:47 PM
Post #4


Newbie [Level 3]
***

Group: Members
Posts: 47
Joined: 22-July 07
From: Dhaka, Bangladesh
Member No.: 46,859



Dear osknockout,
Is frok() is smiler to "Thread" [in java]. You told that
QUOTE
fork() creates a process running the program. If you had two cpu's, you'd theoretically fork a program once to have parts of it running on both.
. I am very eager to know what is it.
Go to the top of the page
 
+Quote Post
osknockout
post Aug 10 2007, 11:59 PM
Post #5


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



Yeah, fork() basically is Thread - if you're talking about the public class in Java.
A process and a thread are technically different things, but the words are used almost interchangably at times.

Technically, you have one process running with multiple threads, but with the fork() command, the aim is to
make threads using miniature processes. Tell me if that makes sense.
Go to the top of the page
 
+Quote Post
rajibbd
post Aug 11 2007, 04:41 PM
Post #6


Newbie [Level 3]
***

Group: Members
Posts: 47
Joined: 22-July 07
From: Dhaka, Bangladesh
Member No.: 46,859



This is pretty much better. Thank you Mr. Osknockout. I have some idea about Thread in Java. Now Know similer use of Thread[though it is not similer]. But any way thank you again.
Go to the top of the page
 
+Quote Post
osknockout
post Aug 12 2007, 04:19 PM
Post #7


Super Member
*********

Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



@rajibbd: No problem, here to help. ^^

@sonalurfrnd: Is the code still a problem or is this topic basically over...?
Go to the top of the page
 
+Quote Post
syncn21
post Sep 26 2007, 11:50 AM
Post #8


Newbie [Level 1]
*

Group: Members
Posts: 19
Joined: 26-November 06
Member No.: 34,064



Basically fork() is used to create a new process in UNIX shell Programming .
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Chilled Salad Fork(0)


 



-