newguy2004
asked on
Need someone for linux C code compiler & Fork() Code Problem
1) I dont have a linux C compiler..so 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");
exit(0);
}
else if (p==0)
{
execlp(command,command,NUL L);
printf("Whoa! the child could do %s!\n",command);
exit(-1);
}
if (should_wait == true)
{
wait(NULL);
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_qui t()
{
char line[MAX_LINE_LEN] ;
printf("What next? ");
fgets(line,MAX_LINE_LEN,st din);
char* ptr = strchr(line,'\n');
if (ptr != NULL)
*ptr = '\0';
do_command (line,should_wait);
return false;
}
int main()
{
while(1)
{
get_and_do_commands_until_ should_qui t();
}
return 0;
}
************************** ********** ********** ********** **
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");
exit(0);
}
else if (p==0)
{
execlp(command,command,NUL
printf("Whoa! the child could do %s!\n",command);
exit(-1);
}
if (should_wait == true)
{
wait(NULL);
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_
{
char line[MAX_LINE_LEN] ;
printf("What next? ");
fgets(line,MAX_LINE_LEN,st
char* ptr = strchr(line,'\n');
if (ptr != NULL)
*ptr = '\0';
do_command (line,should_wait);
return false;
}
int main()
{
while(1)
{
get_and_do_commands_until_
}
return 0;
}
**************************
ASKER
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 :)
compiled with g++ -Wall yourprog.cpp
No error, no warning
Congratulations :)
Oh by the way, it works :)
ASKER
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_qui t() command, it is incomplete, and im lost there.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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..
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
test.c: In function `get_and_do_commands_until
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)