Skip to content
Browse files

Merge branch 'master' of github.com:gosudream/learn-c

  • Loading branch information...
2 parents 4e2fb3e + 01d4128 commit 90bd51a054c38a0d53928f37965e1a9e831ce4fa @gosudream committed
View
9 apue2/process/get_login_name.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int main()
+{
+ //DON'T use $LOGNAME, `cas environment variables can be modified
+ printf("current user is: %s\n", getlogin());
+ return 0;
+}
View
11 apue2/process/process_group_test.c
@@ -0,0 +1,11 @@
+#include <unistd.h>
+#include <stdio.h>
+
+int main()
+{
+ printf("pid is: %d\n", getpid());
+ printf("process group is[getpgrp()]:%d.\n", getpgrp());
+ printf("process group is[getpgid()]:%d.\n", getpgid(0));
+
+ return 0;
+}
View
47 apue2/process/process_times.c
@@ -0,0 +1,47 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/times.h>
+
+static void pr_times(clock_t time, struct tms * start, struct tms * end);
+static void do_cmd(char *cmdstr);
+
+int main(int argc, char *argv[])
+{
+ int i = 0;
+ setbuf(stdout, NULL);
+ for (i = 1; i < argc; i++)
+ do_cmd(argv[i]); // once for each command-line arg
+ return 0;
+}
+
+static void do_cmd(char *cmdstr)
+{
+ struct tms tmsstart, tmsend;
+ clock_t start, end;
+
+ int status;
+ printf("\ncommand: %s\n", cmdstr);
+ start = times(&tmsstart);
+ status = system(cmdstr);
+ end = times(&tmsend);
+
+ pr_times(end -start, &tmsstart, &tmsend);
+}
+
+static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
+{
+ static long clktck = 0;
+ if (clktck == 0)
+ clktck = sysconf(_SC_CLK_TCK);
+ printf("\treal: %7.2f\n", real / (double)clktck);
+ printf("\tuser: %7.2f\n",
+ (tmsend->tms_utime - tmsstart->tms_utime) / (double) clktck);
+ printf("\tsys : %7.2f\n",
+ (tmsend->tms_stime - tmsstart->tms_stime) / (double) clktck);
+ printf("\tchild user: %7.2f\n",
+ (tmsend->tms_cutime - tmsstart->tms_cutime) / (double) clktck);
+ printf("\tchild sys : %7.2f\n",
+ (tmsend->tms_cstime - tmsstart->tms_cstime) / (double) clktck);
+}
+
+
View
51 apue2/retval.c
@@ -0,0 +1,51 @@
+#include "../common.h"
+#include <pthread.h>
+
+struct foo {
+ int a, b, c, d;
+};
+
+void printfoo(const char *s, const struct foo *fp) {
+ printf(s);
+ printf(" structure at 0x%x\n", (unsigned)fp);
+ printf(" foo.a = %d\n", fp->a);
+ printf(" foo.b = %d\n", fp->b);
+ printf(" foo.c = %d\n", fp->c);
+ printf(" foo.d = %d\n", fp->d);
+}
+
+void * thr_fn1(void *arg)
+{
+ struct foo foo = {1, 2, 3, 4};
+ printfoo("thread 1:\n", &foo);
+ pthread_exit((void *)&foo);
+}
+
+void *thr_fn2(void *arg) {
+ printf("thread 2: ID is %d\n", pthread_self());
+ pthread_exit((void *)0);
+}
+
+int main(void)
+{
+ pthread_t tid1, tid2;
+ int err;
+ struct foo *fp;
+
+ err = pthread_create(&tid1, NULL, thr_fn1, NULL);
+ if (err != 0)
+ err_quit("can't create tread 1: %s\n", strerror(err));
+ err = pthread_join(tid1, (void *)&fp);
+ if (err != 0)
+ err_quit("can't join with thread 1: %s\n", strerror(err));
+ printfoo("parent: \n", fp);
+ sleep(1);
+
+ printf("parent starting second thread.\n");
+ err = pthread_create(&tid2, NULL, thr_fn2, NULL);
+ if (err != 0)
+ err_quit("can't create tread 2: %s\n", strerror(err));
+ sleep(1);
+ printfoo("parent: \n", fp);
+ exit(0);
+}
View
33 apue2/signal/nonreentrant.c
@@ -0,0 +1,33 @@
+#include "../common.h"
+#include <pwd.h>
+static sigcount = 0;
+static void send_usr1()
+{
+ sleep(1);
+ kill(getpid(), SIGUSR1);
+}
+
+static void my_alarm(int signo)
+{
+ struct passwd *rootptr;
+ printf("in signal handler: %d\n", sigcount++);
+ if ((rootptr = getpwnam("root")) == NULL)
+ err_sys("getpwnam(root)");
+ send_usr1();
+}
+
+int main()
+{
+ struct passwd *ptr;
+ printf("process id is: %d\n", getpid());
+ signal(SIGUSR1, my_alarm);
+ send_usr1();
+ for(; ;) {
+ if ((ptr = getpwnam("gosudream")) == NULL)
+ err_sys("getpwnam(gosudream)");
+ if (strcmp(ptr->pw_name, "gosudream") != 0)
+ printf("return value corrupted! pw_name = %s\n", ptr->pw_name);
+ }
+
+ return 0;
+}
View
38 apue2/signal/signal_pending.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+
+static void sig_quit(int signo);
+
+int main(void)
+{
+ sigset_t newmask, oldmask, pendmask;
+
+ signal(SIGQUIT, sig_quit);
+
+ // block SIGQUIT
+ sigemptyset(&newmask);
+ sigaddset(&newmask, SIGQUIT);
+ sigprocmask(SIG_BLOCK, &newmask, &oldmask);
+
+ sleep(5);
+
+ // SIGQUIT will remain pending
+ sigpending(&pendmask);
+ if (sigismember(&pendmask, SIGQUIT))
+ printf("\nSIGQUIT pending\n");
+
+ // reset sigmask which unblocks SIGQUIT
+ sigprocmask(SIG_SETMASK, &oldmask, NULL);
+ printf("SIGQUIT unblocked\n");
+
+ sleep(5);
+ exit(0);
+}
+
+static void sig_quit(int signo)
+{
+ printf("Caught SIGQUIT\n");
+ signal(SIGQUIT, SIG_DFL);
+}
View
19 apue2/signal/signal_test.c
@@ -0,0 +1,19 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <signal.h>
+
+static void signal_handler(int signo)
+{
+ printf("USR1 received, signo is: %d.\n", signo);
+}
+
+int main()
+{
+ signal(SIGUSR1, signal_handler);
+ printf("signal test, pid is: %d.\n", getpid());
+
+ while (1)
+ pause();
+
+ return 0;
+}
View
33 apue2/thread/thread_exit.c
@@ -0,0 +1,33 @@
+#include "../common.h"
+#include <pthread.h>
+
+void *thr_fn1(void *arg)
+{
+ printf("thread 1 returning.\n");
+ return (void *) 1;
+}
+
+void *thr_fn2(void *arg)
+{
+ printf("thread 2 returning.\n");
+ return (void *) 2 ;
+}
+
+int main(void)
+{
+ int err;
+ pthread_t tid1, tid2;
+ void *tret;
+
+ err = pthread_create(&tid1, NULL, thr_fn1, NULL);
+ // no err check
+
+ pthread_create(&tid2, NULL, thr_fn2, NULL);
+ pthread_join(tid1, &tret);
+ printf("thread 1 exit code %d\n", (int)tret);
+
+ pthread_join(tid2, &tret);
+ printf("thread 2 exit code %d\n", (int)tret);
+
+ exit(0);
+}
View
35 apue2/thread/thread_id.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <pthread.h>
+
+pthread_t ntid;
+
+void printids(const char *s)
+{
+ pid_t pid;
+ pthread_t tid;
+
+ pid = getpid();
+ tid = pthread_self();
+ printf("%s pid %u tid %u (ox%x)\n", s, (unsigned int)pid,
+ (unsigned int)tid, (unsigned int)tid);
+}
+
+void *thr_fn(void *arg)
+{
+ printids("new thread:\t");
+ return NULL;
+}
+
+int main(void)
+{
+ int err;
+ err = pthread_create(&ntid, NULL, thr_fn, NULL);
+ if (err != 0)
+ printf("error create thread\n");
+
+ printids("main thread:\t");
+ sleep(1);
+ exit(0);
+}
View
13 linuxbyexample/getenv.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+extern char **environ;
+
+int main(void) {
+ int i;
+ if (environ != NULL) { // actually, unnecessary
+ for (i = 0; environ[i] != NULL; i++) {
+ printf("%s\n", environ[i]);
+ }
+ }
+ return 0;
+}
View
32 linuxbyexample/getopt.c
@@ -0,0 +1,32 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv) {
+ int oc; // option character
+ //char *b_opt_arg;
+ //opterr = 0;
+
+ while ((oc = getopt(argc, argv, ":ab:")) != -1) {
+ switch (oc) {
+ case 'a':
+ printf("there is an A\n");
+ break;
+ case 'b':
+ printf("b option is: %s\n", optarg);
+ break;
+ case '?':
+ printf("got an ?: invalid opt:%c\n", optopt);
+ break;
+ case ':':
+ printf("got a :, missing option argument: %c\n", optopt);
+ break;
+ default:
+ printf("error\n");
+ }
+ }
+
+ printf("optind is %d\n", optind);
+
+ exit(0);
+}
View
13 misc/printid.c
@@ -0,0 +1,13 @@
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdio.h>
+int main(void) {
+ printf(
+ " UID GID \n"
+ "Real %d Real %d \n"
+ "Effective %d Effective %d \n",
+ getuid (), getgid (),
+ geteuid(), getegid()
+ );
+ return 0; /* always good to return something */
+}
View
13 misc/process_identities.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char** argv)
+{
+ printf("pid: %d\n", getpid());
+ printf("parent pid: %d\n", getppid());
+ printf("real user id: %d\n", getuid());
+ printf("effective user id: %d\n", geteuid());
+ printf("group id: %d\n", getgid());
+
+ return 0;
+}

0 comments on commit 90bd51a

Please sign in to comment.
Something went wrong with that request. Please try again.