|
我这里有一个问题.一个共享资源在同一时刻只能由一个访问者,用信号量实现.但是如果这个资源正在被访问,另外一个立即退出.
请问应该怎么做?
我这里的代码如下,请问应该怎样修改
[code:1]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <errno.h>
#include <linux/types.h>
#include <linux/sem.h>
#include <linux/ipc.h>
#include <wait.h>
#include <time.h>
#define HT_HONEYPOT_PORT 5320
#define HT_MAX_CLIENT_NUM 1
#define SEM_ID 250
int sem_lock(int sem_set_id)
{
struct sembuf sem_op;
sem_op.sem_num = 0;
sem_op.sem_op = -1;
sem_op.sem_flg = IPC_NOWAIT;
return semop(sem_set_id, &sem_op, 1);
}
int sem_unlock(int sem_set_id)
{
struct sembuf sem_op;
sem_op.sem_num = 0;
sem_op.sem_op = 1;
sem_op.sem_flg = 0;
return semop(sem_set_id, &sem_op, 1);
}
int main(void)
{
int listen_fd, connect_fd;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
int rc;
int sin_size;
char hello[] = "Hello! Are You Fine?";
int sem_set_id;
union semun sem_val;
pid_t pid;
sem_set_id = semget(SEM_ID, 1, IPC_CREAT|0600);
if(sem_set_id == -1)
{
perror("Error: semget()");
exit(1);
}
sem_val.val = 1;
if( semctl(sem_set_id, 0, SETVAL, sem_val) == -1)
{
perror("main: semctl()");
exit(1);
}
if((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Error: socket()");
exit(1);
}
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(HT_HONEYPOT_PORT);
if(bind(listen_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1)
{
perror("Bind error\n");
exit(1);
}
if(listen(listen_fd, 5) == -1)
{
perror("Listen Error");
exit(1);
}
while(1)
{
sin_size = sizeof(struct sockaddr_in);
if((connect_fd = accept(listen_fd, (struct sockaddr *)(&client_addr), &sin_size)) == -1)
{
perror("Accept error");
exit(1);
}
fprintf(stderr, "Server get connection from %s\n",
inet_ntoa(client_addr.sin_addr));
if( (pid = fork()) == 0)
{
close(listen_fd);
if(sem_lock(sem_set_id) != EAGAIN)
{
int i;
for(i = 0; i < 10; i++)
{
sleep(2);
if(write(connect_fd, hello, strlen(hello)) == -1)
{
fprintf(stderr, "Write error");
close(connect_fd);
sem_unlock(sem_set_id);
exit(1);
}
}
close(connect_fd);
sem_unlock(sem_set_id);
exit(0);
}
else
{
fprintf(stdout, "Some Body have conncected\n");
close(connect_fd);
}
}
}
close(listen_fd);
return 0;
}
[/code:1] |
|