文件可以根据其存放数据的作用的不同,将其分为普通文件、目录文件、链接文件、设备文件和管道文件
1. vfork
/* * File: main.c * Author: root * * Created on 2013年11月1日, 上午9:34 */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> /* * */ int gvar=2; int main(int argc, char** argv) { pid_t pid; int var=5; printf("process id:%ld\n",(long)getpid()); if((pid=vfork())<0) { perror("error!"); return 1; }else if(pid==0) { gvar--; var++; printf("the child process id:%ld\n gvar=%d var =%d \n",(long)getpid(),gvar,var); _exit(0); }else{ printf("the parent process id:%ld \n gvar=%d var=%d \n",(long)getpid(),gvar,var); return 0; } return (EXIT_SUCCESS); }
2.share memory
#include<stdio.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/shm.h> int main() { int shmid; int proj_id; key_t key; int size; char *addr; pid_t pid; key=IPC_PRIVATE; //创建共享内存 shmid=shmget(key,1024,IPC_CREAT|0660); if(shmid==-1) { perror("create share memory failed!"); return 1; } //挂载 addr=(char*)shmat(shmid,NULL,0); if(addr==(char *)(-1)) { perror("cannot attach!"); return 1; } printf("share memory segment's address:%x\n",addr); //拷贝数据到共享内存 strcpy(addr,"welcome to mrsoft!"); //创建子进程 pid=fork(); if(pid==-1) { perror("error!!!!"); return 1; } else if(pid==0) { //子进程 printf("child process string is' %s'\n",addr); _exit(0); } else { wait(NULL); printf("parent process string is '%s'\n",addr); //释放共享内存,反挂载 if(shmdt(addr)==-1) { perror("release failed!"); return 1; } //共享内存控制操作 if(shmctl(shmid,IPC_RMID,NULL)==-1) { perror("failed!"); return 1; } } return 0; }
3. TestSize
/* * File: main.c * Author: root * * Created on 2013年11月13日, 下午3:07 */ #include <stdio.h> #include <stdlib.h> #include <limits.h> #include <stdint.h> int main(int argc, char** argv) { printf("CHAR_BIT is %d \n",CHAR_BIT); printf("sizeof(char) is %d \n",sizeof(char)); printf("sizeof(short) is %d \n",sizeof(short)); printf("sizeof(int) is %d \n",sizeof(int)); printf("sizeof(long) is %d \n",sizeof(long)); printf("sizeof(long long) is %d \n\n",sizeof(long long)); // 8 1 2 4 4 8 printf("sizeof(int8_t) is %d \n",sizeof(int8_t)); printf("sizeof(int16_t) is %d \n",sizeof(int16_t)); printf("sizeof(int32_t) is %d \n",sizeof(int32_t)); printf("sizeof(int64_t) is %d \n\n",sizeof(int64_t)); // 1 2 4 8 printf("sizeof(uint8_t) is %d \n",sizeof(uint8_t)); printf("sizeof(uint16_t) is %d \n",sizeof(uint16_t)); printf("sizeof(uint32_t) is %d \n",sizeof(uint32_t)); printf("sizeof(uint64_t) is %d \n",sizeof(uint64_t)); //1 2 4 8 return (EXIT_SUCCESS); }
4. getMac
#include <stdio.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <netinet/in.h> #include <net/if.h> #include <string.h> int main(int argc, char *argv[]) { struct ifreq ifreq; int sock; if(argc!=2) { printf("Usage : ethname\n"); return 1; } if((sock=socket(AF_INET,SOCK_STREAM,0))<0) { perror("socket"); return 2; } strcpy(ifreq.ifr_name,argv[1]); if(ioctl(sock,SIOCGIFHWADDR,&ifreq)<0) { perror("ioctl error"); return 3; } printf("%02x:%02x:%02x:%02x:%02x:%02x\n", (unsigned char)ifreq.ifr_hwaddr.sa_data[0], (unsigned char)ifreq.ifr_hwaddr.sa_data[1], (unsigned char)ifreq.ifr_hwaddr.sa_data[2], (unsigned char)ifreq.ifr_hwaddr.sa_data[3], (unsigned char)ifreq.ifr_hwaddr.sa_data[4], (unsigned char)ifreq.ifr_hwaddr.sa_data[5]); close(sock); return 0; }
5.pthread
/* * File: main.c * Author: root * * Created on 2013年11月13日, 下午5:25 */ #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <string.h> #include <sys/types.h> pthread_t ptid; void printtid(const char* s) { pid_t pid; pthread_t tid; pid=getpid(); tid=pthread_self(); printf("%s pid=%u tid=%u (0x%x)\n",s,(unsigned int )pid,(unsigned int )tid,(unsigned int )tid); } void *thr_fn(void *arg) { printtid("new thread:"); return (void *)0; } int main(int argc, char** argv) { int err; err=pthread_create(&ptid,NULL,thr_fn,NULL); if(err!=0) { printf("can't create thread:%s\n",strerror(err)); return -1; } printtid("main thread:"); sleep(1); return (EXIT_SUCCESS); }