全部博文(40)
2011年(40)
分类: LINUX
2011-09-05 10:05:34
# vi show.sh
内容为:
#!/bin/bash
i=0
while [ $i -lt 10 ]
do
for j in '-' '\\' '|' '/'
do
echo -ne "\033[1D$j"
sleep 1
done
((i++))
done
8-2 实验二 文件操作实验
源码:
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1024
int main(int argc,char **argv)
{
int from_fd,to_fd;
int bytes_read,bytes_write;
char buffer[BUFFER_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)
{
fprintf(stderr,"Open %s Error:%s\n",argv[1],strerror(errno));
exit(1);
}
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
{
fprintf(stderr,"Open %s Error:%s\n",argv[2],strerror(errno));
exit(1);
}
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
{
if((bytes_read==-1)&&(errno!=EINTR)) break;
else if(bytes_read>0)
{
ptr=buffer;
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);
exit(0);
}
8-3 实验三 多线程实验
源码:
#include
#include
#include
#include
void *thread_function(void *arg);
int main(int argc,char **argv)
{
int res;
pthread_t a_thread;
void *thread_result;
int i;
res = pthread_create(&a_thread,NULL,thread_function,NULL);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
for(i=0; i<5; i++)
{
printf("this is main ......\n");
sleep(1);
}
printf("Cancelling thread...\n");
res = pthread_cancel(a_thread);
if (res != 0)
{
perror("thread cancelation failed");
exit(EXIT_FAILURE);
}
printf("waiting for thread to finish...\n");
#if 1
res = pthread_join(a_thread,&thread_result);
if (res != 0)
{
perror("thread join failed");
exit(EXIT_FAILURE);
}
#endif
printf("the progam is end\n");
return 0;
}
void *thread_function(void *arg)
{
int i,res,j;
printf("it is thread \n");
//sleep(1);
res = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,NULL);
if (res != 0)
{
perror("thread pthread_setcancelstate failed");
exit(EXIT_FAILURE);
}
sleep(2);
printf("thread cancel type is disable,can't cancel this thread\n");
for (i = 0;i < 3;i ++)
{
printf("thread is running (%d)...\n",i);
sleep(1);
}
printf("thread is running (%d)...\n",i);
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
#if 1
if (res != 0)
{
perror("thread pthread_setcancelstate failed");
exit(EXIT_FAILURE);
}
#endif
// sleep(5);
printf("thread is change to cancel enable\n");
printf("thread is change \n");
sleep(100);
pthread_exit(0);
}
8-4 实验四 多进程实验源码:
#include
#include
#include
#include
int main(int argc,char ** argv)
{
pid_t pid;
int res;
if ((pid = fork()) < 0)
printf("error");
else if (pid == 0)
{
printf("this child \n");
res = execl("/bin/ls","ls","-l","./",(char *)0);
if (res == -1)
{
perror("execl");
exit(EXIT_FAILURE);
}
exit(1);
}
else
{
sleep(3);
waitpid(pid,NULL);
printf("father ok!\n");
}
return 0;
}
8-5 实验五 进程间通信实验
接收端:
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char ** argv)
{
int running = 1;
char *shm_p = NULL;
int shmid;
int semid;
int value;
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_flg = SEM_UNDO;
if ((semid = semget((key_t)123456,1,0666 | IPC_CREAT)) == -1)
{
perror("semget");
exit(EXIT_FAILURE);
}
shmid = shmget((key_t)654321,(size_t)2048,0600 | IPC_CREAT);
if (shmid == -1)
{
perror("shmget");
exit(EXIT_FAILURE);
}
shm_p = shmat(shmid,NULL,0);
if (shm_p == NULL)
{
perror("shmat");
exit(EXIT_FAILURE);
}
while (running)
{
if ((value = semctl(semid,0,GETVAL)) == 1)
{
printf("read data operate\n");
sem_b.sem_op = -1;
if (semop(semid,&sem_b,1) == -1)
{
fprintf(stderr,"semaphore_p failed\n");
exit(EXIT_FAILURE);
}
printf("%s\n",shm_p);
}
if (strcmp(shm_p,"end") == 0)
running--;
}
shmdt(shm_p);
if (shmctl(shmid,IPC_RMID,0) != 0)
{
perror("shmctl");
exit(EXIT_FAILURE);
}
if (semctl(semid,0,IPC_RMID,0) != 0)
{
perror("semctl");
exit(EXIT_FAILURE);
}
return 0;
}
发送端:
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char ** argv)
{
int running = 1;
int shid;
int semid;
int value;
void * sharem = NULL;
struct sembuf sem_b;
sem_b.sem_num = 0;
sem_b.sem_flg = SEM_UNDO;
if ((semid = semget((key_t)123456,1,0666|IPC_CREAT)) == -1)
{
perror("semget");
exit(EXIT_FAILURE);
}
if (semctl(semid,0,SETVAL,0) == -1)
{
printf("sem init error");
if (semctl(semid,0,IPC_RMID,0) != 0)
{
perror("semctl");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
shid = shmget((key_t)654321,(size_t)2048,0600|IPC_CREAT);
if (shid == -1)
{
perror("shmget");
exit(EXIT_FAILURE);
}
sharem = shmat(shid,NULL,0);
if (sharem == NULL)
{
perror("sharem");
exit(EXIT_FAILURE);
}
while (running)
{
if ((value = semctl(semid,0,GETVAL)) == 0)
{
printf("write data opreate\n");
printf("please input something:");
scanf("%s",sharem);
printf("you input is :%s\n",sharem);
sem_b.sem_op = 1;
if (semop(semid,&sem_b,1) == -1)
{
fprintf(stderr,"semaphore_p failed\n");
exit(EXIT_FAILURE);
}
}
if (strcmp(sharem,"end") == 0)
running--;
}
shmdt(sharem);
return 0;
}
可以把其中一端作为后台程序(添加“&”)。
8-6 实验六 网络编程实验服务端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MYPORT 1234 // the port users will be connecting to
#define BACKLOG 5 // how many pending connections queue will hold
#define BUF_SIZE 200
int fd_A[BACKLOG]; // accepted connection fd
int conn_amount; // current connection amount
void showclient()
{
int i;
printf("client amount: %d\n", conn_amount);
for (i = 0; i < BACKLOG; i++) {
printf("[%d]:%d ", i, fd_A[i]);
}
printf("\n\n");
}
int main(void)
{
int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd
struct sockaddr_in server_addr; // server address information
struct sockaddr_in client_addr; // connector's address information
socklen_t sin_size;
int yes = 1;
char buf[BUF_SIZE];
int ret;
int i;
if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
server_addr.sin_family = AF_INET; // host byte order
server_addr.sin_port = htons(MYPORT); // short, network byte order
server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sock_fd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
printf("listen port %d\n", MYPORT);
fd_set fdsr;
int maxsock;
struct timeval tv;
conn_amount = 0;
sin_size = sizeof(client_addr);
maxsock = sock_fd;
while (1) {
// initialize file descriptor set
FD_ZERO(&fdsr);
FD_SET(sock_fd, &fdsr);
// timeout setting
tv.tv_sec = 30;
tv.tv_usec = 0;
// add active connection to fd set
for (i = 0; i < BACKLOG; i++) {
if (fd_A[i] != 0) {
FD_SET(fd_A[i], &fdsr);
}
}
ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv);
if (ret < 0) {
perror("select");
break;
} else if (ret == 0) {
printf("timeout\n");
continue;
}
// check every fd in the set
for (i = 0; i < conn_amount; i++) {
if (FD_ISSET(fd_A[i], &fdsr)) {
ret = recv(fd_A[i], buf, sizeof(buf), 0);
if (ret <= 0) { // client close
printf("client[%d] close\n", i);
close(fd_A[i]);
FD_CLR(fd_A[i], &fdsr);
fd_A[i] = 0;
} else { // receive data
if (ret < BUF_SIZE)
memset(&buf[ret], '\0', 1);
printf("client[%d] send:%s\n", i, buf);
}
}
}
// check whether a new connection comes
if (FD_ISSET(sock_fd, &fdsr)) {
new_fd = accept(sock_fd, (struct sockaddr *)&client_addr, &sin_size);
if (new_fd <= 0) {
perror("accept");
continue;
}
// add to fd queue
if (conn_amount < BACKLOG) {
fd_A[conn_amount++] = new_fd;
printf("new connection client[%d] %s:%d\n", conn_amount,
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
if (new_fd > maxsock)
maxsock = new_fd;
}
else {
printf("max connections arrive, exit\n");
send(new_fd, "bye", 4, 0);
close(new_fd);
break;
}
}
showclient();
}
// close other connections
for (i = 0; i < BACKLOG; i++) {
if (fd_A[i] != 0) {
close(fd_A[i]);
}
}
exit(0);
}
客户端:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERVERIP "192.168.1.100"
#define SERVERPORT 1234
#define MAXDATASIZE 256
#define STDIN 0
int main(void)
{
int sockfd;
int recvbytes;
char buf[MAXDATASIZE];
char send_str[MAXDATASIZE];
struct sockaddr_in serv_addr;
fd_set rfd_set, wfd_set, efd_set;
struct timeval timeout;
int ret;
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
perror("socket");
exit(1);
}
bzero(&serv_addr, sizeof(struct sockaddr_in));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVERPORT);
inet_aton(SERVERIP, &serv_addr.sin_addr);
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1 )
{
perror("connect");
exit(1);
}
//fcntl(sockfd, F_SETFD, O_NONBLOCK);
printf("");
while(1)
{
FD_ZERO(&rfd_set);
FD_ZERO(&wfd_set);
FD_ZERO(&efd_set);
FD_SET(STDIN, &rfd_set);
FD_SET(sockfd, &rfd_set);
FD_SET(sockfd, &efd_set);
timeout.tv_sec = 30;
timeout.tv_usec = 0;
ret = select( sockfd+1, &rfd_set, &wfd_set, &efd_set, &timeout);
if(ret == 0)
{
continue;
}
if(ret < 0)
{
perror("select error:");
}
if(FD_ISSET(STDIN, &rfd_set))
{
fgets(send_str, 256, stdin);
send_str[strlen(send_str)-1] = '\0';
if( strncmp("quit", send_str, 4) == 0 )
{
close(sockfd);
exit(0);
}
send(sockfd, send_str, strlen(send_str), 0);
}
if(FD_ISSET(sockfd, &rfd_set))
{
recvbytes = recv(sockfd, buf, MAXDATASIZE, 0);
if(recvbytes == 0)
{
close(sockfd);
exit(0);
}
buf[recvbytes] = '\0';
printf("Server: %s\n", buf);
fflush(stdout);
}
if(FD_ISSET(sockfd, &efd_set))
{
printf("efd_set\n");
close(sockfd);
exit(0);
}
}
}
可以把其中一端作为后台程序(添加“&”)。