/*
* =====================================================================================
*
* Filename: fa.c
*
* Description:
*
* Version: 1.0
* Created: 06/25/2014 08:04:55 AM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <time.h>
struct mymsg
{
long mtype; /* message type, must be > 0 */
char mtext[180];
};
/* 获得文件大小 */
unsigned long get_file_size (const char *path)
{
unsigned long filesize = 0;
struct stat statbuff;
if (stat (path, &statbuff) < 0)
{
return filesize;
}
else
{
filesize = statbuff.st_size;
return filesize;
}
}
int cpfile(char *filename , char *newfilename)
{
int fd1 = 0, fd2 = 0, res = 0, filesize = 0;
fd1 = open (filename, O_RDONLY);
if (fd1 == -1)
{
perror ("open1");
return -1;
}
filesize = get_file_size(filename);
printf ("file size=%d\n", filesize);
char *buf = malloc (filesize+1);
memset (buf, 0, filesize+1);
res = read (fd1, buf, filesize);
if (res == -1)
{
perror ("read");
return -1;
}
printf ("read size=%d\n", res);
char *fullfilename = malloc (512);
memset (fullfilename, 0, 512);
char *dir = "/tmp/capturefile";
sprintf (fullfilename, "%s/%s", dir, newfilename);
fd2 = open (fullfilename, S_IRWXU | O_CREAT | O_RDWR | O_TRUNC);
if (fd2 == -1)
{
perror ("open2");
return -1;
}
res = write (fd2, buf, filesize);
if (res == -1)
{
perror ("write-fa.c-78");
return -1;
}
if (buf)
{
free(buf);
buf = NULL;
}
close (fd1);
close (fd2);
}
#define KEY (key_t)0x1fff
int
main ( int argc, char *argv[] )
{
key_t key;
int msgid;
int res;
key = KEY;
struct mymsg msg;
printf ("发送给解析进程......\n");
strcpy (msg.mtext, "haha");
/* 创建消息队列 */
msgid = msgget (key, 0666 | IPC_CREAT);
if (msgid == -1)
{
perror ("msgget");
}
msg.mtype = 1;
int i = 0;
while((i++) < 4000000)
{
res = msgsnd (msgid, &msg, sizeof(struct mymsg), 0);
printf ("fa ok msg.mtext = %s\n", msg.mtext);
}
return 0;
}