Click here to Skip to main content
15,918,177 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Two classes Pin
J Patel18-Jun-02 11:28
J Patel18-Jun-02 11:28 
GeneralRe: Two classes Pin
Michael Mac19-Jun-02 1:52
Michael Mac19-Jun-02 1:52 
GeneralRe: Two classes Pin
Maciej Pirog19-Jun-02 1:27
Maciej Pirog19-Jun-02 1:27 
GeneralRe: Two classes Pin
Michael Mac19-Jun-02 1:49
Michael Mac19-Jun-02 1:49 
GeneralRe: Two classes Pin
Maciej Pirog19-Jun-02 4:10
Maciej Pirog19-Jun-02 4:10 
GeneralRe: Two classes Pin
Michael Mac19-Jun-02 6:30
Michael Mac19-Jun-02 6:30 
GeneralMC++ Competition - any last minute developers want to team up. Pin
TigerNinja_16-Jun-02 16:44
TigerNinja_16-Jun-02 16:44 
Generali need help!!!!! Pin
ChiYung15-Jun-02 16:20
ChiYung15-Jun-02 16:20 
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!!!!! Pin
David Wengier15-Jun-02 16:51
David Wengier15-Jun-02 16:51 
GeneralRe: i need help!!!!! Pin
Rama Krishna Vavilala15-Jun-02 18:13
Rama Krishna Vavilala15-Jun-02 18:13 
GeneralRe: i need help!!!!! Pin
David Wengier15-Jun-02 18:21
David Wengier15-Jun-02 18:21 
GeneralHeap management in a multithreaded windows application Pin
sailesh14-Jun-02 20:29
sailesh14-Jun-02 20:29 
GeneralRe: Heap management in a multithreaded windows application Pin
James T. Johnson14-Jun-02 20:42
James T. Johnson14-Jun-02 20:42 
GeneralSome hope for MC++ coders Pin
Nish Nishant13-Jun-02 19:55
sitebuilderNish Nishant13-Jun-02 19:55 
GeneralRe: Some hope for MC++ coders Pin
Derek Lakin13-Jun-02 20:06
Derek Lakin13-Jun-02 20:06 
GeneralRe: Some hope for MC++ coders Pin
Nish Nishant13-Jun-02 20:25
sitebuilderNish Nishant13-Jun-02 20:25 
GeneralRe: Some hope for MC++ coders Pin
Alexandru Savescu13-Jun-02 21:52
Alexandru Savescu13-Jun-02 21:52 
GeneralRe: Some hope for MC++ coders Pin
Nish Nishant13-Jun-02 21:59
sitebuilderNish Nishant13-Jun-02 21:59 
GeneralRe: Some hope for MC++ coders Pin
Michael P Butler13-Jun-02 22:08
Michael P Butler13-Jun-02 22:08 
GeneralRe: Some hope for MC++ coders Pin
Nish Nishant13-Jun-02 22:21
sitebuilderNish Nishant13-Jun-02 22:21 
GeneralRe: Some hope for MC++ coders Pin
James T. Johnson13-Jun-02 22:23
James T. Johnson13-Jun-02 22:23 
GeneralRe: Some hope for MC++ coders Pin
Nish Nishant13-Jun-02 22:26
sitebuilderNish Nishant13-Jun-02 22:26 
GeneralRe: Some hope for MC++ coders Pin
Michael P Butler13-Jun-02 22:07
Michael P Butler13-Jun-02 22:07 
GeneralRe: Some hope for MC++ coders Pin
Rama Krishna Vavilala13-Jun-02 22:05
Rama Krishna Vavilala13-Jun-02 22:05 
GeneralRe: Some hope for MC++ coders Pin
James T. Johnson13-Jun-02 22:14
James T. Johnson13-Jun-02 22:14 

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.