在命名管道上编程

时间:2014-10-22 08:30:20

标签: c unix pipe named

这是我的逻辑:我正在使用银行管理程序,在该程序中创建帐户时,它将显示在另一侧"帐户创建"及其必要的细节。 然后在主文件中,如果存款,它应显示另一方的存款金额和当前余额

我的问题是我一次只能做一个,我想检查是存款还是创建并为两者做手术

继承我的代码:

计划:a.c

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

void deposit();
int fd,fd1;
char * myfifo = "myfifo";
char * myfifo1 = "myfifo1";
int accno;
char name[15];
int balance;

void create();

int main()
{
    int ch;
    while(1)
    {
        printf("\n*********************");
        printf("\n BANKING ");
        printf("\n*********************");
        printf("\n1-Creation"); 
        printf("\n2-Deposit");
        printf("\n3-Exit");
        printf("\nEnter your choice");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1: create();
            break;
            case 2: deposit();
            break;
            case 3: exit(0);
            break;
            default: printf("Enter 1-3 only");
            break;
        }
    }
    return 0;
}

void create(){
    fd1 = open("bank.txt", O_CREAT|O_WRONLY);

    printf("\nEnter The Account Number\n");
    scanf("%d",&accno);
    write(fd1,&accno,sizeof(accno));
    printf("\nAccount:%d",accno);
    printf("\nEnter The Name\n");
    scanf("%s",&name);
    write(fd1,&name,sizeof(name));
    printf("\nName:%s",name);
    printf("\nEnter The Balance\n");
    scanf("%d",&balance);
    write(fd1,&balance,sizeof(balance));
    printf("\nBalance Available:%d",balance);

    close(fd1);

    mkfifo(myfifo, 0666);
    fd1 = open("bank.txt",O_RDONLY);

    fd = open(myfifo, O_WRONLY);

    read(fd1,&accno,sizeof(accno));
    write(fd,&accno,sizeof(accno));
    printf("\nAccount:%d",accno);
    read(fd1,&name,sizeof(name));
    write(fd,&name,sizeof(name));
    printf("\nName:%s",name);
    read(fd1,&balance,sizeof(balance));
    write(fd,&balance,sizeof(balance));
    printf("\nBalance Available:%d",balance);

    close(fd);
    close(fd1);

    /* remove the FIFO */
    unlink(myfifo);
}

void deposit()
{
    int i=5, acc,b=0,m=0,n=8;
    float aa;
    fd1 = open("bank.txt", O_RDWR);

    printf("\n*************************************");
    printf("\n DEPOSIT ");
    printf("\n*************************************");
    printf("\nEnter your Account Number");
    scanf("%d",&acc);
    for(b=0;b<i;b++)
    {
        if(accno == acc)
        m = n; 
    }

    if(accno == acc){
        printf("\n Account Number : %d",accno);
        printf("\n Name : %s",name);
        printf("\n Deposit : %d",balance);
        printf("\n How Much Deposited Now:");
        scanf("%f",&aa);
        balance=balance+aa;
        write(fd1,&balance,sizeof(balance));                                                                                                                                                                     
        printf("\nDeposit Amount is :%f",balance);

        mkfifo(myfifo1, 0666);

        fd = open(myfifo1, O_WRONLY);                                                                                                                                                                                      
        read(fd1,&balance,sizeof(balance));
        write(fd,&balance,sizeof(balance));
        printf("\nBalance Available:%d",balance);
        close(fd);
        close(fd1);
        unlink(myfifo1);
    }
    else
    {                                                                                                                                      
        printf("\nACCOUNT NUMBER IS INVALID");                                                                                                                                                                                
    }
}

计划:b.c

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_BUF 1024

int main()
{
    int fd,fd1;
    char * myfifo = "myfifo";
    char * myfifo1 = "myfifo1";                                                                                                                                                                                
    int accno;
    char name[15];
    int balance;
    /* open, read, and display the message from the FIFO */
    fd = open(myfifo, O_RDONLY);

    printf("Account Created\n----------------\n");

    read(fd,&accno,sizeof(accno));
    printf("Account Number: %d\n", accno);

    read(fd,&name,sizeof(name));
    printf("Name: %s\n", name);

    read(fd,&balance,sizeof(balance));
    printf("Balance: %d\n", balance);

    close(fd);                                                                                                                                                                                  
    fd1 = open(myfifo1, O_RDONLY);                                                                                                                                                                                 
    printf("Deposited\n----------------\n");
    read(fd1,&balance,sizeof(balance));
    printf("Balance: %d\n", balance);                                                                                                                                                                                
    close(fd1);
} 

这里在程序文件b.c中我想检查现在正在执行哪一个并且只继续那个,即如果在程序a.c中选择了存款,那么只存入应该显示在prog b.c中

0 个答案:

没有答案