Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. i need help!!!!!

i need help!!!!!

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++sysadminregextutorial
4 Posts 3 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ChiYung
    wrote on last edited by
    #1

    Hi, I know it is a VC++ 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: :-O

    D 1 Reply Last reply
    0
    • C ChiYung

      Hi, I know it is a VC++ 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: :-O

      D Offline
      D Offline
      David Wengier
      wrote on last edited by
      #2

      Hahahaha. You're an idiot.

      R 1 Reply Last reply
      0
      • D David Wengier

        Hahahaha. You're an idiot.

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        David Wengier wrote: Hahahaha. You're an idiot. At first I thought that it was pretty rude. Now I understand what is going on. I think the guy is playing.

        D 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          David Wengier wrote: Hahahaha. You're an idiot. At first I thought that it was pretty rude. Now I understand what is going on. I think the guy is playing.

          D Offline
          D Offline
          David Wengier
          wrote on last edited by
          #4

          Rama Krishna wrote: At first I thought that it was pretty rude I think my reply was very rude.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups