Skip to content

Commit 9cdc7c5

Browse files
committed
ENH: allowed parent, child process concurrency
1 parent 3cf04b0 commit 9cdc7c5

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed
13.7 KB
Binary file not shown.
13.7 KB
Binary file not shown.
13.7 KB
Binary file not shown.

3-Process Concept/project - shell/aman_shell.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
#include <sys/types.h>
22
#include <stdio.h>
33
#include <unistd.h>
4+
#include <string.h>
45
#include <stdlib.h>
56

7+
typedef int bool;
8+
9+
#define true 1
10+
#define false 0
611
#define LSH_RL_BUFSIZE 1024 /* buffer size for reading user input */
712
#define LSH_TOK_BUFSIZE 64
813
#define LSH_TOK_DELIM " \t\r\n\a"
914

15+
/* global variable to check parent and child process concurrency */
16+
bool conc = false;
17+
1018
/* Function declarations for built-in shell commands */
1119
int lsh_cd(char **args);
1220
int lsh_help(char **args);
@@ -46,7 +54,7 @@ int lsh_cd(char **args)
4654
int lsh_help(char **args)
4755
{
4856
int i;
49-
printf("Aman Dalmia's LSH\n", );
57+
printf("Aman Dalmia's LSH\n");
5058
printf("Type program names and arguments, and press enter\n");
5159
printf("The following are built in:\n");
5260

@@ -73,13 +81,16 @@ int lsh_launch(char **args){
7381
if(execvp(args[0], args) == -1) perror("lsh");
7482
exit(EXIT_FAILURE);
7583
}else if(pid > 0){ /* parent process */
76-
do{
77-
wpid = waitpid(pid, &status, WUNTRACED);
78-
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
84+
if(!conc){
85+
do{
86+
wpid = waitpid(pid, &status, WUNTRACED);
87+
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
88+
}
7989
}else{ /* error forking */
8090
perror("lsh");
8191
}
8292

93+
conc = false;
8394
return 1;
8495
}
8596

@@ -102,7 +113,7 @@ int lsh_execute(char **args){
102113

103114
/* Parse input to get the arguments */
104115
char **lsh_split_line(char *line){
105-
int bufsize = LSH_TOK_DELIM, position = 0;
116+
int bufsize = LSH_TOK_BUFSIZE, position = 0;
106117
char **tokens = malloc(bufsize * sizeof(char*));
107118
char *token;
108119

@@ -127,6 +138,10 @@ char **lsh_split_line(char *line){
127138

128139
token = strtok(NULL, LSH_TOK_DELIM);
129140
}
141+
if(strcmp(tokens[position - 1], "&") == 0) {
142+
conc = true;
143+
tokens[position - 1] = NULL;
144+
}
130145
tokens[position] = NULL;
131146
return tokens;
132147
}
@@ -146,7 +161,7 @@ char *lsh_read_line(void)
146161

147162
while(1){
148163
/* Read a character */
149-
c = getchar()
164+
c = getchar();
150165

151166
if(c == EOF || c == '\n'){
152167
buffer[position] = '\0';
@@ -178,8 +193,8 @@ void lsh_loop(void)
178193
do {
179194
printf(">");
180195
line = lsh_read_line();
181-
args = lsh_split_line();
182-
status = lsh_execute(line);
196+
args = lsh_split_line(line);
197+
status = lsh_execute(args);
183198

184199
free(line);
185200
free(args);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
3+
int main(void)
4+
{
5+
int p1[10];
6+
int *p2[10];
7+
int (*p3)[10];
8+
9+
printf("sizeof(int) = %d\n", (int)sizeof(int));
10+
printf("sizeof(int *) = %d\n", (int)sizeof(int *));
11+
printf("sizeof(p1) = %d\n", (int)sizeof(p1));
12+
printf("sizeof(p2) = %d\n", (int)sizeof(p2));
13+
printf("sizeof(p3) = %d\n", (int)sizeof(p3));
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)