Context Switching question

This is a discussion on Context Switching question within the C Programming forums, part of the General Programming Boards category; There are 2 threat to run at the same time. For example, there are number 1 and number 2, how ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Context Switching question

    There are 2 threat to run at the same time. For example, there are number 1 and number 2, how can I run it at the same time. my coding still exit some problem, number 2 can not restore the previous number after I press ctrl^c, can anyone help me to debug it,thanks a lot:

    Code:
    #include <dos.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <conio.h>
    jmp_buf buf_main, buf_p1, buf_p2;
    void processOne();
    void processTwo();
    void main(void)
    {
       char line[81];
       char option;
       int retVal;
       for (;;)
       {
       retVal=setjmp(buf_main);
       printf("\nEnter process number to execute the process, 'e' to exit: ");
       gets(line);
       option=line[0];
       switch(option)
       { case '1':
           if (retVal==0)
               processOne();
           else
               longjmp(buf_p1,1);
           break;
    
         case '2':
           if (retVal==1)
               processTwo();
           else
               longjmp(buf_p2,1);
           break;
    
         case 'e':
           exit(0);
           break;
       }
       }
    }
    
    void processOne()
    {
       int breakRoutine();
       register clocktick=0;
       ctrlbrk(breakRoutine);
       for (;;)
       { setjmp(buf_p1);
           clrscr();
           printf("Clock of process 1 is %d", ++clocktick);
           delay(500);
       }
    }
    
    void processTwo()
    {
       int breakRoutine();
       register clocktick=0;
       ctrlbrk(breakRoutine);
       for (;;)
       { setjmp(buf_p2);
           clrscr();
           printf("Clock of process 2 is %d", ++clocktick);
           delay(500);
       }
    }
    
    int breakRoutine()
    {
       longjmp(buf_main,1);
       return 1;
    
    }
    Last edited by Dave_Sinkula; 09-28-2006 at 10:54 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This code is absolutely horrible, outdated, unsafe, non-standard, and there's nothing concurrent about it.
    Oh, and it's more C than C++, so you posted on the wrong board.

    If you want real multithreading, get a proper compiler, not some DOS thing (DOS does not have multithreading), then learn an appropriate threading library: pthreads on Linux, Win32 threads on Win32, or perhaps a cross-platform wrapper around them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Quote Originally Posted by tongboy
    ...how can I run it at the same time...
    You can't unles you have two processors. With one processor, it is not possible to run two threads at the same. But you can make a precedence among the threads.

    And...get a newer compiler!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads + Context Switching
    By Nositi in forum C Programming
    Replies: 6
    Last Post: 02-18-2008, 08:42 AM
  2. how to get context switching between threads
    By v3dant in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 11:04 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  4. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21