Click here to Skip to main content
16,012,223 members
Home / Discussions / C#
   

C#

 
GeneralRe: getting a disabled picture Pin
Rüpel17-Jun-02 3:39
Rüpel17-Jun-02 3:39 
GeneralGetting favourite list of Internet Explorer dynamically..... Pin
Pranoti16-Jun-02 23:08
Pranoti16-Jun-02 23:08 
GeneralRe: Getting favourite list of Internet Explorer dynamically..... Pin
Eric Gunnerson (msft)17-Jun-02 5:59
Eric Gunnerson (msft)17-Jun-02 5:59 
General.Net remoting Pin
HG16-Jun-02 9:17
HG16-Jun-02 9:17 
GeneralRe: .Net remoting Pin
Jared Bienz17-Jun-02 6:34
Jared Bienz17-Jun-02 6:34 
GeneralProblem with using SHGetFileInfo to retrieve icons Pin
Paul Ingles16-Jun-02 1:00
Paul Ingles16-Jun-02 1:00 
GeneralRe: Problem with using SHGetFileInfo to retrieve icons Pin
Jared Bienz17-Jun-02 6:43
Jared Bienz17-Jun-02 6:43 
Generali need help!!!! Pin
ChiYung15-Jun-02 16:21
ChiYung15-Jun-02 16:21 
Hi,

I know it is a C# forum, but i really need help!!! Let's see my code first:

*****************************************************************************
void operation(int[], char[]);
void waitprocess(int);
void main(void)
{
        int i, j;
    static char buffer[BUFSIZE+10], X_value[5], message[BUFSIZE];
    int f_des[2];

    if (pipe(f_des) == -1)   //create the pipe
      {
        perror("Pipe");
        exit(2);
      }

    if (fork()==0)    //In the CHILD
    {
      // do nothing here because we will 
      // create 5 CHILDs by PARENT again later.    
    }
    else            //PARENT
    {
      for (j=1; j<=5; j++)   //create 5 CHILD process
        {
          if (fork()==0)  
        {
          static char temp[BUFSIZE];
          sprintf(temp, "Thread %d", getpid());    //Get the thread ID
          operation(f_des, temp);                  //Send the request
        }
        }

        while (X>0)     // keep doing (read from pipe) until X=0    
        {
        close(f_des[1]);
        if (read(f_des[0], message, BUFSIZE) != -1)  //Read from pipe
          {
            sprintf(buffer, " - X  = %d \n", X);
            strcat(message, buffer);
            write(1, message, sizeof(message));    //display output
            waitprocess(getpid());  //force sleep awhile
            X--;
          }
        else
          {
            printf("error");
          }
        } 
    }
}
****************************************************************************


I have to create 5 "threads" but using fork() because i have to use pipe() to communicate with "server". This program is doing that 5 "threads" are trying to decrement the global X until X=0. I use pipe as a mechanisam to protect this "critical section". I don't know whether i did it right or wrong. Please help me to check!!!

Also, I found that the output looks strange. The output is always in a pattern. For example, the output looks liked:

Thread 1001 - X = 20
Thread 1003 - X = 19
Thread 1002 - X = 18
Thread 1005 - X = 17
Thread 1004 - X = 16
Thread 1001 - X = 15
Thread 1003 - X = 14
Thread 1002 - X = 13
Thread 1005 - X = 12
Thread 1004 - X = 11
*
*
*

You can see the sequence is 1,3,2,5,4,1,3,2,5,4...etc. I think the output should be in random sequence. Why it's happened???

Thanks!!!!!Confused | :confused: Blush | :O
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 16:42
protectorChristian Graus15-Jun-02 16:42 
GeneralRe: i need help!!!! Pin
James T. Johnson15-Jun-02 16:54
James T. Johnson15-Jun-02 16:54 
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 17:19
protectorChristian Graus15-Jun-02 17:19 
GeneralRe: i need help!!!! Pin
Rama Krishna Vavilala15-Jun-02 18:19
Rama Krishna Vavilala15-Jun-02 18:19 
GeneralRe: i need help!!!! Pin
Martin Marvinski15-Jun-02 17:46
Martin Marvinski15-Jun-02 17:46 
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 17:42
protectorChristian Graus15-Jun-02 17:42 
GeneralRe: i need help!!!! Pin
Martin Marvinski15-Jun-02 18:06
Martin Marvinski15-Jun-02 18:06 
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 18:04
protectorChristian Graus15-Jun-02 18:04 
GeneralRe: i need help!!!! Pin
Martin Marvinski15-Jun-02 18:07
Martin Marvinski15-Jun-02 18:07 
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 18:12
protectorChristian Graus15-Jun-02 18:12 
GeneralRe: i need help!!!! Pin
Rama Krishna Vavilala15-Jun-02 18:12
Rama Krishna Vavilala15-Jun-02 18:12 
GeneralRe: i need help!!!! Pin
Christian Graus15-Jun-02 18:13
protectorChristian Graus15-Jun-02 18:13 
GeneralInherited events Pin
14-Jun-02 9:28
suss14-Jun-02 9:28 
GeneralRe: Inherited events Pin
14-Jun-02 10:06
suss14-Jun-02 10:06 
GeneralRe: Inherited events Pin
James T. Johnson14-Jun-02 14:03
James T. Johnson14-Jun-02 14:03 
GeneralRe: Inherited events Pin
Rama Krishna Vavilala15-Jun-02 3:08
Rama Krishna Vavilala15-Jun-02 3:08 
GeneralHost name for SMTP server... Pin
14-Jun-02 4:58
suss14-Jun-02 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.