Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a9ac7af

Browse files
committed
Added examples of working with other process attributes.
1 parent 70cb8eb commit a9ac7af

File tree

3 files changed

+206
-0
lines changed

3 files changed

+206
-0
lines changed

‎multithreading/01 - Env Vars.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "Common.h"
4+
5+
extern char ** environ; // Global variable availble to any process for read/write.
6+
7+
#define ENVVARSAPP "ENVVARSAPP=test"
8+
9+
#define OVERWRITE 1
10+
11+
// Compilation:
12+
// gcc -std=gnu99 "01 - Env Vars.c" -o envVars
13+
14+
// Task:
15+
// Read, write environment variables.
16+
17+
int main(int argc, char *argv[])
18+
{
19+
printf("Variables available process:\n");
20+
for (int i = 0; environ[i] != NULL; i++)
21+
{
22+
printf("%s\n", environ[i]);
23+
}
24+
25+
// Get HOME variable
26+
char *s = getenv("HOME");
27+
if (s == NULL)
28+
{
29+
printf("HOME not found.\n");
30+
}
31+
else
32+
{
33+
printf("Value of HOME: \"%s\"\n", s);
34+
}
35+
36+
// Put new variable.
37+
// WARNING! Don't put local or automatic non-static variables since it will be used by system.
38+
if (putenv(ENVVARSAPP) == FAILURE)
39+
{
40+
perror("putenv ENVVARSAPP failed!\n");
41+
}
42+
else
43+
{
44+
printf("putenv ENVVARSAPP succeeded!\n");
45+
}
46+
47+
// Set new variable.
48+
if (setenv("ENVVARSAPP2", "test2", OVERWRITE) == FAILURE)
49+
{
50+
perror("setenv ENVVARSAPP2 failed!\n");
51+
}
52+
else
53+
{
54+
printf("setenv ENVVARSAPP2 succeeded!\n");
55+
}
56+
57+
// Remove variables.
58+
if (unsetenv("ENVVARSAPP") == FAILURE)
59+
{
60+
perror("unsetenv ENVVARSAPP failed!\n");
61+
}
62+
else
63+
{
64+
printf("unsetenv ENVVARSAPP succeeded!\n");
65+
}
66+
67+
if (unsetenv("ENVVARSAPP2") == FAILURE)
68+
{
69+
perror("unsetenv ENVVARSAPP2 failed!\n");
70+
}
71+
else
72+
{
73+
printf("unsetenv ENVVARSAPP2 succeeded!\n");
74+
}
75+
76+
printf("Exit process!\n");
77+
exit(EXIT_SUCCESS);
78+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <sys/wait.h> // For wait, waitpid
6+
7+
#include "Common.h"
8+
9+
// Compilation:
10+
// gcc -std=gnu99 "02 - Parent Process Die.c" -o parentDie
11+
12+
// Task:
13+
// Use fork to create a child process. Emulate parent process died and Parent PID should become Init (1) or some other?
14+
15+
const char* get_process_name_by_pid(const int pid)
16+
{
17+
char* name = (char*)calloc(1024,sizeof(char));
18+
if(name)
19+
{
20+
sprintf(name, "/proc/%d/cmdline",pid);
21+
FILE* f = fopen(name,"r");
22+
if(f)
23+
{
24+
size_t size;
25+
size = fread(name, sizeof(char), 1024, f);
26+
if(size>0)
27+
{
28+
if('\n'==name[size-1])
29+
{
30+
name[size-1]='0円';
31+
}
32+
}
33+
fclose(f);
34+
}
35+
}
36+
return name;
37+
}
38+
39+
int main(int argc, char *argv[])
40+
{
41+
printf("Enter to main!\n");
42+
printf("Create 1 child process...\n");
43+
pid_t pid;
44+
int status;
45+
switch(pid=fork())
46+
{
47+
case -1:
48+
perror("1 Error of calling fork");
49+
exit(EXIT_FAILURE);
50+
case 0:
51+
printf("1 CHILD: Child process %d!\n", getpid());
52+
printf("2 CHILD: Parent PID %d!\n", getppid());
53+
printf("3 CHILD: Wait 3 seconds...\n");
54+
sleep(3);
55+
pid_t ppid = getppid();
56+
printf("4 CHILD: Looks like PARENT died! What is my parent pid? %d\n", ppid); // TODO Find Parent Process Name.
57+
printf("5 CHILD: My parent now is: %s\n", get_process_name_by_pid(ppid)); // TODO Find Parent Process Name.
58+
printf("6 CHILD: Wow! Cool! Exit!\n");
59+
exit(EXIT_SUCCESS);
60+
default:
61+
printf("1 PARENT: Parent process %d!\n", getpid());
62+
printf("2 PARENT: Child PID %d\n", pid);
63+
printf("3 PARENT: Wait 2 seconds...\n");
64+
sleep(2);
65+
printf("4 PARENT: Dying... bye-bye child process!\n");
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
#include <sys/wait.h> // For wait, waitpid
6+
7+
#include "Common.h"
8+
9+
// Compilation:
10+
// gcc -std=gnu99 "03 - Process attributes.c" -o processAttrs
11+
12+
// Task:
13+
// Work with process attributes.
14+
15+
int main(int argc, char *argv[])
16+
{
17+
printf("Enter to main!\n");
18+
printf("Create 1 child process...\n");
19+
pid_t pid;
20+
int status;
21+
switch(pid=fork())
22+
{
23+
case -1:
24+
perror("1 Error of calling fork");
25+
exit(EXIT_FAILURE);
26+
case 0:
27+
printf("1 CHILD: Child process %d!\n", getpid());
28+
printf("2 CHILD: Parent PID %d!\n", getppid());
29+
printf("3 CHILD: Who created me? User ID: %d\n", getuid());
30+
printf("3 CHILD: Who created me? Effective User ID: %d\n", geteuid());
31+
printf("3 CHILD: Who created me? Group ID: %d\n", getgid());
32+
printf("3 CHILD: Who created me? Effective Group ID: %d\n", getegid());
33+
34+
// WARNING!! Works only under ROOT.
35+
// Changing root from / to some another directory (Example: restrict access to directories more than Web Server directories for Security).
36+
//printf("4 CHILD: Let's change Working Dir: %d\n", chdir(""));
37+
//printf("4 CHILD: Let's change Working Dir: %d\n", fchdir(""));
38+
//printf("4 CHILD: Let's change Root Dir: %d\n", chroot(""));
39+
// Process priority is in range N=[0, 39]. Nice returns N-20. In case of error Nice returns -1. If N == 19 ir returns -1 TOO :).
40+
printf("5 CHILD: Let's decrease Priority Level for N=5: %d (N-20)\n", nice(5));
41+
printf("5 CHILD: Let's increase Priority Level for N=-5: %d (N-20)\n", nice(-5));
42+
43+
printf("6 CHILD: Wait 4 seconds...\n");
44+
sleep(4);
45+
printf("7 CHILD: Exit!\n");
46+
exit(EXIT_FAILURE);
47+
default:
48+
printf("1 PARENT: Parent process %d!\n", getpid());
49+
printf("2 PARENT: Child PID %d\n", pid);
50+
printf("3 PARENT: Who created me? User ID: %d\n", getuid());
51+
printf("3 PARENT: Who created me? Group ID: %d\n", getgid());
52+
printf("3 PARENT: Who created me? Effective (under which permissions) User ID: %d\n", geteuid());
53+
printf("3 PARENT: Who created me? Effective (under which permissions) Group ID: %d\n", getegid());
54+
waitpid(pid, &status, 0);
55+
printf("4 PARENT: Child exit code: %d\n", WEXITSTATUS(status));
56+
printf("5 PARENT: Exit!\n");
57+
}
58+
59+
printf("Exit process!\n");
60+
exit(EXIT_SUCCESS);
61+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /