Skip to content

Instantly share code, notes, and snippets.

@Prototyped
Last active July 29, 2022 08:20
Show Gist options
  • Select an option

  • Save Prototyped/d4e9ec6505bfadf1340cea891f5bc02e to your computer and use it in GitHub Desktop.

Select an option

Save Prototyped/d4e9ec6505bfadf1340cea891f5bc02e to your computer and use it in GitHub Desktop.
argv
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
pid_t pid;
if (argc > 1) {
fputs("Execed child with argv[0] ", stdout);
fputs(argv[0], stdout);
fputs(" and argv[1] ", stdout);
fputs(argv[1], stdout);
fputc('\n', stdout);
sleep(10);
return 0;
}
pid = fork();
if (pid) {
char command_line[80];
snprintf(command_line, sizeof(command_line), "ps wup %d", pid);
fputs("Forked parent, running ps\n", stdout);
system(command_line);
} else {
fputs("Forked child, execing self\n", stdout);
execl(argv[0], "ooga-booga", "child", NULL);
}
return 0;
}
# amitg @ athena in ~ [9:17:56]
% gcc -g -Wall -c fancy-argv0.c
# amitg @ athena in ~ [9:17:58]
% gcc -g -o fancy-argv0 fancy-argv0.o
# amitg @ athena in ~ [9:18:00]
% ./fancy-argv0
Forked parent, running ps
Forked child, execing self
Execed child with argv[0] ooga-booga and argv[1] child
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
amitg 32659 0.0 0.0 2420 732 pts/5 S+ 09:18 0:00 ooga-booga child
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment