Like Tree1Likes
  • 1 Post By dmh2000

an practice problem on C and OS

This is a discussion on an practice problem on C and OS within the C Programming forums, part of the General Programming Boards category; I am learning OS and using nachos as an instruction tool. Anyway, here is a question. The following code will ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    a practice problem on C and OS

    I am learning OS and using nachos as an instruction tool. Anyway, here is a question. The following code will lead to a "segmentation fault". Specifically, the program runs well until the thread which prints "D" finishes (reaches the upper limit).
    Do you have any idea why? Thanks.
    Code:
    /* nice_free.c 
     *   Test program to run several threads with different priorities.
     *   Each thread uses the NICE system call to adjust its static 
     *   priority by various amounts, to influence execution order. 
     *
     *   This version of nice does not use the console, it uses a
     *   debugging system call, Echo, instead.
     */
    
    #include "syscall.h"
    #include "stdlib.h"
    
    #define OUTPUT_LOOP_LIMIT 500
    #define A_NICE 0
    #define B_NICE 5
    #define C_NICE 10
    #define D_NICE 15
    
    int
    main()
    {
        int a,b,c,d;
        int pid;
    
        Echo ("Starting...\n", 12);
    
        pid = Fork();
    
        if (pid == 0)
          {
    
        pid = Fork(); 
    
        if (pid != 0) {
    
          pid = Fork(); 
          if (pid != 0) {
    
            Nice (A_NICE);
                for (a = 0; a < OUTPUT_LOOP_LIMIT; a++) {
              Echo ("A", 1);
                }
            Exit (0);
          } else {
    
            Nice (B_NICE);
                for (b = 0; b < OUTPUT_LOOP_LIMIT; b++) {
              Echo ("B", 1);
                }
            Exit (0);
          }
        } else {
    
          pid = Fork(); 
          if (pid != 0) {
    
            Nice (C_NICE);
                for (c = 0; c < OUTPUT_LOOP_LIMIT; c++) {
              Echo ("C", 1);
                }
            Exit (0);
          } else {
    
            Nice (D_NICE);
                for (d = 0; d < OUTPUT_LOOP_LIMIT; d++) {
              Echo ("D", 1);
                }
            Exit (0);
          }
        }
          }
        /*Original Thread*/
        Wait (0);
        Echo ("\nFinished\n", 10);
        Exit(0);        /* and then we're done */
    }
    Last edited by oldercoder; 02-06-2012 at 05:03 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Can you post the definitions of all the functions used in your code viz., Fork(), Nice(), Echo(), etc.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    check the prototype for Fork. what is it in 'syscall.h'?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Thanks for attention.

    Fork() just forks a child process, nothing special.
    Echo() works like fprint(). The number indicates the number of characters. There is no problem there.
    Nice() is used the adjust the static prioty. A higher value gives higher priority to be excuted.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you should have posted the actual prototypes for those calls. its hard to help when you give vague responses. a google search for Nacho syscall.h shows a different signature for Fork than what you are using. I assume there are different versions so that might not matter.
    MK27 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cprogramming practice problem #1
    By joeyj86 in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2012, 11:48 PM
  2. Even or Odd (practice problem)
    By Amphibian in forum C Programming
    Replies: 16
    Last Post: 09-22-2010, 02:32 PM
  3. "uniqe" practice for 8 qeens problem
    By megazord in forum C Programming
    Replies: 21
    Last Post: 11-21-2009, 01:23 PM
  4. inStream practice problem not working?
    By correlcj in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2002, 05:58 PM
  5. Question on an array practice problem
    By Vanished in forum C Programming
    Replies: 1
    Last Post: 01-22-2002, 07:12 PM

Tags for this Thread


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