文件编程
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
int srcid,dstid;
unsigned char buf[128] = {0};
int readlen,pos;
//备份的文件
srcid = open(argv[1],O_RDONLY);
//文件备份名
dstid = open(strcat(argv[1],".bak"),O_WRONLY|O_CREAT);
pos = lseek(srcid,0,SEEK_CUR);
while(readlen = read(srcid,buf,128)>0)
{
write(dstid,buf,readlen);
pos +=readlen;
lseek(srcid,pos,SEEK_SET);
}
printf("\n文件备份成功\n");
close(srcid);
close(dstid);
}
进程编程
fork函数:pid_t fork( void);
成功调用一次则返回两个值,子进程返回0,父进程返回子进程ID;否则,出错返回-1。fork产生子进程的表现就是它会返回2次,一次返回0,顺序执行下面的代码。这是子进程。一次返回子进程的pid,也顺序执行下面的代码,这是父进程。进程创建成功之后,父进程以及子进程都从fork() 之后开始执行,只是pid不同。
Linux 操作系统提供了三个获得进程号的函数 getpid()、getppid()、getpgid()。
函数getpid():pid_t getpid(void); 获取本进程号(PID)
函数getppid():pid_t getppid(void); 获取调用此函数的进程的父进程号(PPID)
函数getpgid():pid_t getpgid(pid_t pid); 获取进程组号(PGID)pid:进程号,参数为 0 时返回当前进程组号,否则返回参数指定的进程的进程组号
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
pid_t child;
int status;
if ((child = fork()) == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if (child == 0)
{
puts("in child");
char *argv[] = {"ls - l ", NULL};
if (execve("/bin/ls", argv, NULL) == -1)
{
perror("execve");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
else
{
waitpid(child, &status, 0);
puts("\nin parent");
printf("\tparent pid = %d\n", getpid());
printf("\tparent ppid = %d\n", getppid());
}
exit(EXIT_SUCCESS);
}
进程间通信
管道、信号量、信号、消息队列、共享内存、套接字
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
static int alarm_fired = 0;
void ding(int sig)
{
alarm_fired = 1;
}
int main()
{
int pid;
printf("alarm application starting\n");
if((pid = fork()) == 0)
{
sleep(5);
kill(getppid(), SIGALRM);
exit(0);
}
printf("waiting for alarm to go off\n");
signal(SIGALRM, ding);
pause();
if (alarm_fired)
printf("Signal is received!\n");
printf("done\n");
exit(0);
}
线程编程
pthread_create 函数
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void *thread_function(void *arg);
char message[] = "Hello World";
int main() {
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if (res != 0) {
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg) {
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
网络编程
tcp_server.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
//1. 申请套接字
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == listenfd)
{
perror("socket");
return -1;
}
//2. 绑定确定的端口与地址(用于客户端目标)
struct sockaddr_in serveraddr = {0};
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(8888);
serveraddr.sin_addr.s_addr = INADDR_ANY;//0
int len = sizeof(serveraddr);
if(-1 == bind(listenfd, (struct sockaddr*)&serveraddr, len) )
{
perror("bind");
return -1;
}
//3. 开启监听
listen(listenfd, 10);
//4. accept(接通接入)
int clientfd = accept(listenfd, NULL, NULL);
while(1)
{
//5. 接受客户端请求
char buf[100] = {0};
read(clientfd, buf, 100);
printf("recv: %s\n", buf);
//your code here
buf[0] -= ' ';
//6. 回复客户端
write(clientfd, buf, 100);
}
//7. 中断链接
close(clientfd);
close(listenfd);
}
tcp_cliect.c
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*短链接*/
int main(int argc, char *argv[])
{
if(3 != argc)
{
printf("Usage: %s <IP> <port>\n", argv[0]);
return -1;
}
//1. 向操作系统申请套接字
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == socketfd)
{
perror("socket create");
return -1;
}
//2. 连接服务器
struct sockaddr_in serveraddr = {0};
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
serveraddr.sin_port = htons(atoi(argv[2]));
int len = sizeof(serveraddr);
if(-1 == connect(socketfd, (struct sockaddr*)&serveraddr, len))
{
perror("connect to server");
return -1;
}
while(1)
{
printf("input: ");fflush(stdout);
char buf[100] = {0};
gets(buf);
//3. 发出请求
write(socketfd, buf, 100);
//4. 接受反馈
read(socketfd, buf, 100);
printf("recv: %s\n", buf);
}
//5. 结束链接
close(socketfd);
}