shell programming using C

Reply

Join Date: Aug 2008
Posts: 2
Reputation: sting_010 is an unknown quantity at this point 
Solved Threads: 0
sting_010 sting_010 is offline Offline
Newbie Poster

shell programming using C

 
0
  #1
Aug 6th, 2008
can anyone in here help me with my programming project.

Your C program must be invoked exactly as follows:

shell [batchFile]

The command line arguments to your shell are to be interpreted as follows.
batchFile: an optional argument (often indicated by square brackets as above). If present, your shell will read each line of the batchFile for commands to be executed. If not present, your shell will run in interactive mode by printing a prompt to the user at stdout and reading the command from stdin.
For example, if you run your program as

shell /u/j/v/batchfile
then your program will read commands from /u/j/v/batchfile until it sees the quit command.

Parsing
For reading lines of input, you may want to look at fgets(). To open a file and get a handle with type FILE * , look into fopen(). Be sure to check the return code of these routines for errors! (If you see an error, the routine perror() is useful for displaying the problem.) You may find the strtok() routine useful for parsing the command line (i.e., for extracting the arguments within a command separated by whitespace or a tab or ...).

Executing Commands
Look into fork(), execvp(), and wait/waitpid().
The fork() system call creates a new process. After this point, two processes will be executing within your code. You will be able to differentiate the child from the parent by looking at the return value of fork; the child sees a 0, the parent sees the pid of the child.
You will note that there are a variety of commands in the exec family; for this project, you must use execvp(). Remember that if execvp() is successful, it will not return; if it does return, there was an error (e.g., the command does not exist). The most challenging part is getting the arguments correctly specified. The first argument specifies the program that should be executed, with the full path specified; this is straight-forward. The second argument, char *argv[] matches those that the program sees in its function prototype:
int main(int argc, char *argv[]);
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: shell programming using C

 
0
  #2
Aug 6th, 2008
Oh, dear, dear. Where's your effort on the matter? No posted code showing what you have tried already towards the assignment; no help you'll receive, I am afraid.
It is not my rule, it is the policy of this board.
Last edited by Aia; Aug 6th, 2008 at 7:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: shell programming using C

 
0
  #3
Aug 7th, 2008
Instead of all that fork/exec stuff, how far can you get with all the command line stuff and parsing, so you can just get to things like

printf( "The command to execute is %s", command );
Until this works reliably, there's no point even worrying about the fork/exec, and it shows us you're not being a total sponge.

Or maybe even something simpler than that. It doesn't matter, so long as you show some actual code effort, we'll help.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 2
Reputation: sting_010 is an unknown quantity at this point 
Solved Threads: 0
sting_010 sting_010 is offline Offline
Newbie Poster

Re: shell programming using C

 
-1
  #4
Aug 7th, 2008
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. /*
  6.  * This program is intended to help you test your shell.
  7.  * You can use it to test that you are correctly starting and waiting
  8.  * for multiple processes.
  9.  *
  10.  * Recommended Usage:
  11.  * Run multiple copies of this program simultaneously
  12.  *
  13.  * You should see the output from each intermingled
  14.  * (you can tell which process is which because each prints its "pid").
  15.  *
  16.  * You might only see the output intermingled when one of the processes
  17.  * goes to sleep; this is okay. However, it is not okay if you see the
  18.  * output of one process completely before the output of the other.
  19.  *
  20.  */
  21. int main(int argc, char *argv[])
  22. {
  23.  
  24. int pid;
  25. int i;
  26. int j;
  27. int outer = 5;
  28. int inner = 10;
  29. int count = 0;
  30. long max_usleep = 1000000; // 1 second
  31. int seed;
  32. long usec;
  33. int c;
  34.  
  35. pid = getpid();
  36. while ((c = getopt(argc, argv, "o:i:s:r:")) != -1) {
  37. switch (c) {
  38. case 'o':
  39. outer = atoi(optarg);
  40. break;
  41.  
  42. case 'i':
  43. inner = atoi(optarg);
  44. break;
  45.  
  46. case 's':
  47. max_usleep = (long)atoi(optarg);
  48. break;
  49.  
  50. case 'r':
  51. seed = atoi(optarg);
  52. srand(seed);
  53. break;
  54.  
  55. default:
  56. fprintf(stderr, "Usage: %s [-o <outerloops>] [-i <innerloops>] [-s <maxusecsleep>] [-r <random seed>]\n", argv[0]);
  57. exit(1);
  58. }
  59. }
  60.  
  61. for (i = 0; i < outer; i++) {
  62.  
  63. for (j = 0; j < inner; j++) {
  64.  
  65. printf("%d: %d\n", pid, count);
  66. count++;
  67. }
  68.  
  69. usec = max_usleep * ((float)rand() / (float)RAND_MAX);
  70. printf("%d sleeping for %ld usec\n", pid, usec);
  71. usleep(usec);
  72. }
  73.  
  74. printf("Job completed\n");
  75. }
  76.  
  77.  
  78. //this is the given program and i have to place my code in this program which i'm unable to do
  79.  
  80.  
  81. // my code v
  82. // v
  83.  
  84. // using fork
  85.  
  86. #include <unistd.h>
  87. #include <stdio.h>
  88. #include <stdlib.h>
  89.  
  90.  
  91. int main(void)
  92. {
  93. pid_t child;
  94.  
  95.  
  96. if((child = fork()) == -1) {
  97. perror("fork");
  98. exit(EXIT_FAILURE);
  99. } else if(child == 0) {
  100. puts("in child");
  101. printf("\tchild pid = %d\n", getpid());
  102. printf("\tchild ppid = %d\n", getppid());
  103. exit(EXIT_SUCCESS);
  104. } else {
  105.  
  106.  
  107. fputs("in parent");
  108. printf("\tparent pid = %d\n", getpid());
  109. printf("\tparent ppid = %d\n", getppid());
  110. }
  111. exit(EXIT_SUCCESS);
  112. }
  113.  
  114.  
  115. // uses the kill function to send two signals to a sleeping child process,
  116. // one that will be ignored, and one that will kill the process
  117.  
  118. #include <sys/types.h>
  119. #include <wait.h>
  120. #include <unistd.h>
  121. #include <signal.h>
  122. #include <stdio.h>
  123. #include <stdlib.h>
  124.  
  125.  
  126. int main(void)
  127. {
  128. pid_t child;
  129. int errret;
  130.  
  131.  
  132. if((child = fork()) < 0) {
  133. perror("fork");
  134. exit(EXIT_FAILURE);
  135. } else if(child == 0) { /* in the child process */
  136. sleep(30);
  137. } else { /* in the parent */
  138. /* send a signal that gets ignored */
  139. printf("sending SIGCHLD to %d\n", child);
  140. errret = kill(child, SIGCHLD);
  141. if(errret < 0)
  142. perror("kill:SIGCHLD");
  143. else
  144. printf("%d still alive\n", child);
  145.  
  146.  
  147. /* now murder the child */
  148. printf("killing %d\n", child);
  149. if((kill(child, SIGTERM)) < 0)
  150. perror("kill:SIGTERM");
  151. /* have to wait to reap the status */
  152. waitpid(child, NULL, 0 );
  153. }
  154. exit(EXIT_SUCCESS);
  155. }
Last edited by WaltP; Aug 7th, 2008 at 8:29 pm. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: shell programming using C

 
0
  #5
Aug 7th, 2008
Another lazy attempt at getting help -- with no Code Tags. Why didnt you read any of the requested information posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) on the background of the box you actually typed you message in
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: shell programming using C

 
0
  #6
Aug 8th, 2008
> this is the given program and i have to place my code in this program which i'm unable to do
More like unable to try.
You've been given 99% of the code, and still you're stuck.

Here's the one line of code
execl( "/bin/ls", "/bin/ls", "-l", (char*)NULL );
Now I'm going to leave just one last bit of mystery to see if you can figure out where that lines goes.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC