}
/* client.c */
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(){
char fn_read[] = "chanel1";
char fn_write[] = "chanel2";
char fn_txt[10];
int fd_write;
int fd_read;
FILE *in_file;
char str[255];
char mes[255];
/* Open fifo chanel */
if( (fd_read = open(fn_read,O_RDONLY)) == NULL ){
printf("from client: can't open %s\n",fn_read);
return 4;
}
if( (fd_write = open(fn_write,O_WRONLY)) == NULL ){
printf("from client: can't open %s\n",fn_write);
return 3;
}
/* CLIENT PART */
if( read(fd_read, fn_txt, sizeof(fn_txt)) == -1 ){
printf("from client: can't read mes\n");
}
else{
printf("from client: File name=%s\n",fn_txt);
}
if((in_file = fopen(fn_txt,"r")) == NULL){
printf("from client: error open file %s\n", fn_txt);
exit(0);
}
/* Read file */
strcpy(mes, "");
while(!feof(in_file)){
fscanf(in_file,"%s", str);
strcat(mes, str);
}
/* Send text from file */
if( write(fd_write, mes, sizeof(mes)) == -1 ){
printf("from client: can't send text from file\n");
return -1;
}
fclose(in_file);
close(fd_read);
close(fd_write);
}
1.4. Разделяемая память.
Server.c
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <sys/mman.h>
#include "shm.h"
int fd;
semaphores *sem;
void int_exit(void);
int main (int argc, char* argv[]){
message *msgptr;
signal(SIGINT,int_exit);
int tmp;
/* Create memory object */
shm_unlink("./mem");
fd = shm_open("./mem", O_RDWR | O_CREAT, 0777 );
if( fd == -1 ){
printf("from server: %s\n",strerror( errno) );
return EXIT_FAILURE;
}
/* Set the memory object's size */
if (ftruncate(fd, sizeof(*msgptr)+sizeof(*sem)) == -1) {
printf("from server: %s\n", strerror(errno));
return EXIT_FAILURE;
}
/* Map the message object */
msgptr = mmap(0, sizeof(*msgptr), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (msgptr == MAP_FAILED) {
printf("mmap failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
printf("msgptr addr is %6.6X\n", msgptr);
printf("size of msgptr = %d\n\n",sizeof(*msgptr));
/* Create the semaphore object */
sem = mmap(0, sizeof(*sem), PROT_READ | PROT_WRITE, MAP_SHARED, fd, sizeof(*msgptr));
if (sem == MAP_FAILED) {
printf("mmap failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
printf("sem addr is %6.6X\n", sem);
printf("size of sem = %d\n\n",sizeof(*sem));
/* Initialize an unnamed semaphore */
if(sem_init(&sem->get,1,0) < 0){
printf("sem_init failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
if(sem_init(&sem->put,1,1) < 0){
printf("sem_init failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
if(sem_getvalue(&sem->get, &tmp) < 0){
printf("sem_getvalue failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
else
printf("sem->get \tvalue = %d\n", tmp);
if(sem_getvalue(&sem->put, &tmp) < 0){
printf("sem_getvalue failed:%s\n", strerror(errno));
return EXIT_FAILURE;
}
else
printf("sem->put \tvalue = %d\n", tmp);
while(1){
sem_wait(&sem->get);
printf("Message from client: %s\n",msgptr->buff);
sem_post(&sem->put);
}
sem_destroy(&sem->put);
sem_destroy(&sem->get);
shm_unlink( "./mem" );
close( fd );
}
void int_exit(){
int tmp;
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.