Skip to content

Commit 0357c1d

Browse files
committed
Add option to run process in background (&)
1 parent ffa26fa commit 0357c1d

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

skerl.c

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ char **skerl_split_command(char *input_command, int *count)
7777
return tokens;
7878
}
7979

80-
int skerl_execute_external_command(char **single_command)
80+
int skerl_execute_external_command(char **single_command, int background)
8181
{
8282
pid_t pid;
8383
int status;
@@ -97,10 +97,18 @@ int skerl_execute_external_command(char **single_command)
9797
}
9898
else
9999
{
100-
do
100+
if (!background)
101101
{
102-
waitpid(pid, &status, WUNTRACED);
103-
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
102+
do
103+
{
104+
waitpid(pid, &status, WUNTRACED);
105+
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
106+
}
107+
else
108+
{
109+
printf("child process with pid [%d] is running as background process\n", pid);
110+
}
111+
104112
}
105113
return 1;
106114
}
@@ -169,12 +177,12 @@ int skerl_globalusage(char **internal_command)
169177
}
170178
else
171179
{
172-
int total = 0,value;
173-
while(fscanf(fptr,"%d",&value)!=EOF)
180+
int total = 0, value;
181+
while (fscanf(fptr, "%d", &value) != EOF)
174182
{
175183
total += value;
176184
}
177-
printf("skerl usage: %d commands :: current session %d commands\n",total,usage);
185+
printf("skerl usage: %d commands :: current session %d commands\n", total, usage);
178186
}
179187
fclose(fptr);
180188
}
@@ -194,20 +202,20 @@ int skerl_averageusage(char **internal_command)
194202
}
195203
else
196204
{
197-
int total = 0,count=0,value;
198-
while(fscanf(fptr,"%d",&value)!=EOF)
205+
int total = 0, count = 0, value;
206+
while (fscanf(fptr, "%d", &value) != EOF)
199207
{
200208
total += value;
201209
count++;
202210
}
203-
float avg_usage = total/count;
204-
float per_utilization = usage/avg_usage*100;
205-
printf("skerl average usage: %f commands :: current session %f\% \n",avg_usage,per_utilization);
211+
float avg_usage = total / count;
212+
float per_utilization = usage / avg_usage * 100;
213+
printf("skerl average usage: %f commands :: current session %f\% \n", avg_usage, per_utilization);
206214
}
207215
fclose(fptr);
208216
}
209217

210-
int skerl_execute(char **single_command)
218+
int skerl_execute(char **single_command, int background)
211219
{
212220
int i;
213221

@@ -223,7 +231,7 @@ int skerl_execute(char **single_command)
223231
return (*execute_builtin_command[i])(single_command);
224232
}
225233
}
226-
return skerl_execute_external_command(single_command);
234+
return skerl_execute_external_command(single_command, background);
227235
}
228236

229237
/* parses the commanfd and tokenizes the input string */
@@ -238,7 +246,14 @@ void skerl_parse_input_command(char *input_command)
238246

239247
for (int i = 1; i < token_count; i++)
240248
{
241-
if ((strcmp(command[i], ">") == 0) || (strcmp(command[i], ">>") == 0))
249+
if (strcmp(command[i], "&") == 0)
250+
{
251+
// run as background process
252+
type = 1;
253+
command[i] = NULL;
254+
skerl_execute(command,1);
255+
}
256+
else if ((strcmp(command[i], ">") == 0) || (strcmp(command[i], ">>") == 0))
242257
{
243258
// output redirection
244259
// stdout -> file
@@ -261,7 +276,7 @@ void skerl_parse_input_command(char *input_command)
261276

262277
int redirect_stream = dup2(output_file, 1);
263278
command[i] = NULL;
264-
skerl_execute(command);
279+
skerl_execute(command,0);
265280
dup2(saved_stdout, 1);
266281
close(saved_stdout);
267282
}
@@ -276,7 +291,7 @@ void skerl_parse_input_command(char *input_command)
276291
int saved_stdin = dup(0);
277292
int redirect_stream = dup2(input_file, 0);
278293
command[i] = NULL;
279-
skerl_execute(command);
294+
skerl_execute(command,0);
280295
dup2(saved_stdin, 0);
281296
close(saved_stdin);
282297
}
@@ -298,7 +313,7 @@ void skerl_parse_input_command(char *input_command)
298313
output_file = open("temp", O_WRONLY);
299314
dup2(output_file, 1);
300315
command[i] = NULL;
301-
skerl_execute(command);
316+
skerl_execute(command,0);
302317
dup2(saved_stdout, 1);
303318
close(saved_stdout);
304319

@@ -307,15 +322,15 @@ void skerl_parse_input_command(char *input_command)
307322
int saved_stdin = dup(0);
308323
dup2(input_file, 0);
309324
char **cmd = &command[i + 1];
310-
skerl_execute(cmd);
325+
skerl_execute(cmd,0);
311326
dup2(saved_stdin, 0);
312327
close(saved_stdin);
313328
remove("temp");
314329
}
315330
}
316331
if (type == 0)
317332
{
318-
skerl_execute(command);
333+
skerl_execute(command,0);
319334
}
320335
}
321336

0 commit comments

Comments
 (0)