Click here to Skip to main content
15,915,328 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: ATL exe server Pin
Hans Ruck16-Jun-02 1:58
Hans Ruck16-Jun-02 1:58 
Generalwin 2000 installation Pin
16-Jun-02 1:02
suss16-Jun-02 1:02 
GeneralRe: win 2000 installation Pin
Mazdak16-Jun-02 1:28
Mazdak16-Jun-02 1:28 
GeneralRe: win 2000 installation Pin
Venet16-Jun-02 7:06
Venet16-Jun-02 7:06 
QuestionAdding resource to Win32 application? Pin
15-Jun-02 19:29
suss15-Jun-02 19:29 
AnswerRe: Adding also icon and version information? Pin
15-Jun-02 19:36
suss15-Jun-02 19:36 
GeneralRe: Adding also icon and version information (all solved!) Pin
15-Jun-02 20:07
suss15-Jun-02 20:07 
Generali need help!!! please............. Pin
ChiYung15-Jun-02 16:22
ChiYung15-Jun-02 16:22 
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 | :confused: Blush | :O
GeneralRe: i need help!!! please............. Pin
Christian Graus15-Jun-02 16:41
protectorChristian Graus15-Jun-02 16:41 
GeneralRe: i need help!!! please............. Pin
Martin Marvinski15-Jun-02 17:52
Martin Marvinski15-Jun-02 17:52 
GeneralRe: i need help!!! please............. Pin
Christian Graus15-Jun-02 18:00
protectorChristian Graus15-Jun-02 18:00 
GeneralRe: i need help!!! please............. Pin
Martin Marvinski15-Jun-02 18:52
Martin Marvinski15-Jun-02 18:52 
GeneralRe: i need help!!! please............. Pin
markkuk16-Jun-02 2:30
markkuk16-Jun-02 2:30 
GeneralLinking problem w/ Intel C++ Pin
15-Jun-02 13:39
suss15-Jun-02 13:39 
GeneralRe: Linking problem w/ Intel C++ Pin
Mike Nordell16-Jun-02 5:07
Mike Nordell16-Jun-02 5:07 
GeneralRe: Linking problem w/ Intel C++ Pin
16-Jun-02 11:52
suss16-Jun-02 11:52 
QuestionWhat causes the error "Incorrect function."? Pin
redeemer15-Jun-02 12:42
redeemer15-Jun-02 12:42 
AnswerRe: What causes the error "Incorrect function."? Pin
Mike Nordell16-Jun-02 5:13
Mike Nordell16-Jun-02 5:13 
QuestionWhy can't i read from a cd-rom drive when i have opened it with CreateFIle? Pin
redeemer15-Jun-02 11:59
redeemer15-Jun-02 11:59 
AnswerRe: Why can't i read from a cd-rom drive when i have opened it with CreateFIle? Pin
JohnnyG15-Jun-02 12:17
JohnnyG15-Jun-02 12:17 
GeneralRe: Why can't i read from a cd-rom drive when i have opened it with CreateFIle? Pin
redeemer15-Jun-02 12:22
redeemer15-Jun-02 12:22 
GeneralRe: Why can't i read from a cd-rom drive when i have opened it with CreateFIle? Pin
Amit Dey15-Jun-02 14:24
Amit Dey15-Jun-02 14:24 
AnswerRe: Why can't i read from a cd-rom drive when i have opened it with CreateFIle? Pin
Amit Dey15-Jun-02 14:22
Amit Dey15-Jun-02 14:22 
Generalstrange... Pin
redeemer15-Jun-02 14:43
redeemer15-Jun-02 14:43 
GeneralRe: strange... Pin
Amit Dey15-Jun-02 15:07
Amit Dey15-Jun-02 15:07 

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.