Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Need someone for linux C code compiler & Fork() Code Problem

1) I dont have a linux C i need someone to compile the code for me and let me know if & where the errors are.

2) This is what im trying to do, and ive done a bit, but not sure if its right. Ive not done the commands part as yet. The 3 commands are List / Count / Quit

1st Function:
- Fork()
- Wait for child to finish if should_wait = true, if its false it should not wait

2nd Function:
- Read line from stdin
- Does commands:
    If the user typed LIST_FILES_COMMAND then it passes "ls" to do_command.
    If the user typed COUNT_COMMAND then it passes "./count" to do_command.
    If the user typed QUIT_COMMAND then it returns true.
    Otherwise it prints "I don't know what \"%s\" means.\n"
    Returns false.
- If user types in WAIT_COMMAND then sets should_wait to true & prints "Baby shell will wait for command to finish."
- If the user types in DONT_WAIT_COMMAND then sets should_wait to false & prints "Baby shell will NOT wait for command to finish."
- Passes should_wait to do_command.

Main function:
- Call 2nd Function until it returns false
- Returns 0 back to the OS

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

using namespace      std;

const      int      MAX_LINE_LEN                  = 256;

#define            WAIT_COMMAND                  "wait"
#define            DONT_WAIT_COMMAND            "dont_wait"
#define            LIST_FILES_COMMAND            "list"
#define            COUNT_COMMAND                  "count"
#define            QUIT_COMMAND                  "quit"

static bool should_wait = false;

void do_command (const char* command, bool should_wait = false)
      int p;
      p = fork();
      if (p==-1)
            printf("Fork Failed");

      else if (p==0)
            printf("Whoa! the child could do %s!\n",command);
      if (should_wait == true)
            printf("Baby shell will wait for command to finish");
      else if  (should_wait == false)
            printf("Baby shell will NOT wait for command to finish");
      printf("Child %d worked on it!\n", getpid());

bool get_and_do_commands_until_should_quit()
      char line[MAX_LINE_LEN] ;
      printf("What next? ");

      char* ptr = strchr(line,'\n');

      if  (ptr != NULL)
      *ptr = '\0';
      do_command (line,should_wait);
      return false;

int main()
      return 0;

  • 3
  • 3
1 Solution
Sjef BosmanGroupware ConsultantCommented:
test.c:8: parse error before `namespace'
test.c:8: warning: data definition has no type or storage class
test.c:18: parse error before `should_wait'
test.c:18: `false' undeclared here (not in a function)
test.c:18: warning: data definition has no type or storage class
test.c:20: parse error before `bool'
test.c: In function `do_command':
test.c:33: `command' undeclared (first use in this function)
test.c:33: (Each undeclared identifier is reported only once
test.c:33: for each function it appears in.)
test.c:38: `true' undeclared (first use in this function)
test.c:43: `false' undeclared (first use in this function)
test.c: At top level:
test.c:52: parse error before `get_and_do_commands_until_should_quit'
test.c: In function `get_and_do_commands_until_should_quit':
test.c:60: parse error before `char'
test.c:62: `ptr' undeclared (first use in this function)
test.c:65: `false' undeclared (first use in this function)
newguy2004Author Commented:
Well, ok so how to complete the function so i can get rid for the errors related with function 1 and 2?
This is a C++ program :)

compiled with g++ -Wall yourprog.cpp

No error, no warning

Congratulations :)
Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

Oh by the way, it works :)
newguy2004Author Commented:
Yea but it doesnt do what its suppose to do. If you see function 2 on my first post, and compare it with the code, thats is the bool get_and_do_commands_until_should_quit() command, it is incomplete, and im lost there.
I think you did the most difficult part,

In get_and_do_commands_until_should_quit()

you just have to compare the input string with your commands

#include <string.h>
static char *commands[] = {     "LIST_FILES_COMMAND",


     char* ptr = strchr(line,'\n');

     if  (ptr != NULL)  *ptr = '\0';

   int i;
   for (i=0 ; i<5 && strcmp(line, commands[i]) ; i++) /* */ ;
  else switch(i)
    case 0: do the list files case ; break;
    case 1: do the count ; break;
    case 3: set should_wait variable to true ; break
    default: printf ("dont understand");

     do_command (line,should_wait);
     return false;
newguy2004Author Commented:
I cant compile it, so i dont know it it will work 100%, but i have to complete this week, so whatever u helped me with, i'll try to put it together..& give u the points..
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now