First, the basic Knowledge Source program compilation
/***hello.c***/
int main () {printf ("hello,world.\n"); return 0;}
Gcc-o Hello hello.c
-O Generate executable file
-C Output only target code
-G to debug
Makefile Writing
# # # #makefile1 # #
MAIN:MAIN.O T1.O T2.O
Gcc-o [email protected] $^
MAIN.O:MAIN.C T1.h T2.h
Gcc-c $<
t1.o:t1.c T1.h
Gcc-c $<
T2.O:T2.C t2.h
Gcc-c $<
[Email protected]# #目标文件
$^# #所有依赖文件
$<# #第一个依赖文件
# # # #makefile2 # #
MAIN:MAIN.O T1.O T2.O
Gcc-o [email protected] $^
.. c.o
Gcc-c $<
# #自动推导
Connection to the Library
-LM Math Library
-lpthread Line Libraries
Default library path for the system:/lib,/usr/lib,/usr/local/lib
Otherwise, specify the connection path:-l/home/ming/mylibs-lming This will connect to the appropriate directory LIBMING.SO|LIBMING.A
Debugging
Refer to GdB Getting Started
Help
Man Write # # #write命令的帮助
Man 2 Write # # #系统调用函数的帮助
Man 3 Write # # #C库函数的帮助
II. concept of process process
A process is the execution of a program, an instance of a program that begins execution but does not end. Each process can have more than one child process ...
To differentiate between different processes, the system assigns a unique ID to each process to differentiate
Status of the process: new, running, blocking, ready, complete
Personal understanding: Multithreading under Linux is essentially a multi-child process, each with its own process ID, but not its own process name, corresponding to the parent process's program name.
View process commands: PS, such as: Ps-ef, Ps-aux
Flag of the process
#include <unistd.h>
pid_t getpid ();//Get Process ID
pid_t getppid ();//get Parent process ID
A process is a service to a program that is serviced by a user. The system establishes a connection for the process and user in order to find the user name of the process. This user becomes the owner of the process, and each user has a user ID. Users belong to different groups, and each group also has its own ID.
#include <unistd.h><sys/types.h>
uid_t getuid ();//Get process Owner user ID
uid_t geteuid ();//obtain a valid user ID for the process
gid_t Getgid ();//Get Group ID
gid_t Getegid ();//get valid group ID
Creation of processes
#include <unistd.h>
pid_t fork ();//Create a process
For the parent process fork () returns the ID of the child process, and for the Fork child process returns 0, the creation process fails back to 1. Once a child process is created, it continues with the parent process from the fork, competing with the system's resources. This can be called by the Wait () or waitpid () function if you want the child process to execute while the parent process blocks until the child process finishes.
After the parent process creates the child process, the child process generally calls a different program. To invoke a system program, you can use the following exec family functions:
#include <unistd.h>
int execl (Char*path,char*arg,...);
int EXECLP (Char*file,char*arg,...);
int execle (Char*path,char*arg,...);
int Execv (char*path,char*argv[]);
int EXECVP (char*file,char*argv[]);
/********forktest.c**********/
#include <unistd.h><sys/types.h><sys/wait.h><stdio.h><errno.h><math.h>
void Main ()
{
pid_t child;
int status;
if ((Child = fork ()) = =-1) exit (1);
else if (child = = 0)
{
int i;
printf ("I am the Child:%ld\n", Getpid ());
for (i=0; i<1000000; i++) sin (i);
Exit (5);
}
while (((Child=wait (&status)) ==-1) && (ERRNO==EINTR));
if (Child = =-1) printf ("Wait Error:%s\n", Strerror (errno));
else if (!status);
else {...}
}
Daemon process
/****checkmail.c****/
#include <unistd.h><sys/types.h><sys/stat.h><stdio.h><errno.h><fcntl.h>< Signal.h>
#define MAIL "/var/spool/mail/ming"
#define SLEEP_TIME 10
void Main ()
{
pid_t child;
if ((Child=fork ()) ==-1) exit (1);
else if (child>0) while (1);
if (Kill (Getppid (), SIGTERM) ==-1) exit (1);
int MAILFD;
while (1)
{
Mailfd=open (mail,o_rdonly);
Close (MAILFD);
Sleep (Sleep_time);
}
}
Third, file operation file Read and write
/*****filerw.h****/
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sting.h>
#define BUF_SIZE 1024
int main (int argc, char** argv)
{
int from_fd, TO_FD;
int Bytes_read, bytes_write;
Char Buf[buf_size];
char* ptr;
if (argc! = 3) {
fprintf (stderr, "Usage:%s fromfile tofile \n\a", argv[0]);
Exit (1);
}
if (from_fd = open (argv[1], o_rdonly)) = =-1) exit (1);
if (to_fd = open (argv[2], o_wronly| o_creat,s_irusr| S_IWUSR)) = =-1) exit (1);
while (Bytes_read = Read (FROM_FD, buf, buf_size))
{
if ((Bytes_read =-1) && (errno! = eintr)) break;
else if (Bytes_read > 0) {
ptr = buf;
while (Bytes_write = write (TO_FD, PTR, Bytes_read))
{
if ((Bytes_write = =-1) && (errno! = eintr)) break;
else if (Bytes_write = = bytes_read) break;
else if (Bytes_write > 0) {
PTR + = Bytes_write;
Bytes_read-= Bytes_write;
}
if (Bytes_write = =-1) break;
}
}
}
Close (FROM_FD);
Close (TO_FD);
return 0;
}
Directory Operations
/******dir_opt.c********/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
static int get_file_size_time (const char* filename)
{
struct stat statbuf;
if (stat (filename, &statbuf) = =-1) return-1;
if (S_isdir (Statbuf.st_mode)) return 1;
if (S_isreg (Statbuf.st_mode)) {
printf ("%s size:%ldbytes \ Modified at%s \ n", filename, Statbuf.st_size, CTime (&statbuf.st_mtime));
}
return 0;
}
int main (int argc, char** argv)
{
dir* Dirp;
struct dirent* direntp;
int stats;
if (argc! = 2) {
printf ("Usage:%s filename \n\a", argv[0]);
Exit (1);
}
if (((stats = get_file_size_time (argv[1)) = = 0) | | (Stats = =-1)) Exit (1);
if ((Dirp = Opendir (argv[1])) = = = NULL) {
Exit (1);
}
while ((DIRENTP = Readdir (dirp)) = NULL);
Closedir (DIRP);
return 0;
}
Pipeline files
/********pipe.c*********/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#define BUFFER 255
int main (int argc, char** argv)
{
Char buffer[buffer+1];
int fd[2];
if (argc! = 2) {
fprintf (stderr, "Usage:%s string\n\a", argv[0]);
Exit (1);
}
if (pipe (FD)! = 0) exit (1);
if (fork () = = 0) {
Close (fd[0]);
printf ("child[%d") write to pipe. \ n ", Getpid ());
snprintf (buffer, buffer, "%s", argv[1]);
Write (fd[1], buffer, strlen (buffer));
printf ("child[%d") quit. \ n ", Getpid ());
Exit (0);
}
else {
Close (fd[1]);
printf ("parent[%d] read from pipe \ n", Getpid ());
memset (buffer, 0, buffer+1);
Read (fd[0], buffer, buffer);
printf ("parent[%d] read:%s\n", getpid (), buffer);
Exit (1);
}
return 0;
}
Iv. Time and time representation
#include <sys/time.h>
time_t time (time_t* tloc);//Returns the number of seconds since 1970
char* ctime (const time_t* clock);//return similar to "Fri 7 13:40:58 2014" string
Gettimeofday ();//Get time in a day
Timer
Todo......
Five, signal processing
Todo
VI. Messaging Management
Todo
VII. Threading Operations
The child process is executed by copying the address space of the parent process, which is executed by the shared program code.
-lpthread Connection Cable Libraries
#include <pthread.h>
int pthread_create (pthread_t* thread, pthread_attr_t* attr, void* (*start_routine) (void*), void* Arg);
void Pthread_exit (void* retval);
int Pthread_join (pthread* thread, void** thread_return);
Eight, network programming
Todo
Linux under C programming Getting started note