Программирование под Linux

Модератор: Модераторы разделов

Magam
Сообщения: 1

Программирование под Linux

Сообщение Magam »

Написал программу-демон для наблюдения за операциями монтирования/размонтирования флоппи-дисковода:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <unistd.h>

int floppy_check()
{
FILE *stream;
char string [1000];
int f=0;

if ((stream = fopen("/proc/mounts", "r"))== NULL)
printf("\nFile /proc/mount wasn`t opened\n");
else
printf("\nFile /proc/mounts was opened\n ");

while (feof(stream)==0)
{
fscanf(stream, "%s", &string);
//printf("%s\n",string);
if (strcmp(string,"/dev/fd0")==0)
f=1;
};

if (f==1)
printf("There is a mounted floppy\n");
else
printf("There isn`t a mounted floppy\n");
fclose(stream);
return f;
}

void create_demon()
{ FILE *stream;
int v;
nice(19);
printf("Creating of demon.PID of demon: %d",getpid());
v = floppy_check();
while(1)
{ if(v != floppy_check())
{ stream = fopen("Report.log","w");
time_t tt;
v != v;
time (&tt);
if (v)
fprintf (stream, "Mounts/dev/fd0 %s", ctime (&tt));
else
fprintf (stream, "Umounts/dev/fd0 %s", ctime (&tt));

fclose(stream);
}
sleep(1);
}
}

int main()
{//int o=flash_check();
//printf("%d\n",o);
pid_t pid;
printf ("PID of main programm: %d\n",getpid());
pid = fork();
if (pid == 0)
create_demon();
return 0;
};

После выполнения откомпилированого файла, предложение - File /proc/mount wasn`t opened или There isn`t a mounted flash, выводиться не один раз, а постоянно. Где допустил ошибку, подскажите?
Спасибо сказали:
Аватара пользователя
DaemonTux
Сообщения: 1480
Статус: Юный падаван
ОС: Gentoo

Re: Программирование под Linux

Сообщение DaemonTux »

Код: Выделить всё

while(1)
{ if(v != floppy_check())
{ stream = fopen("Report.log","w");
time_t tt;
v != v;
time (&tt);
if (v)
fprintf (stream, "Mounts/dev/fd0 %s", ctime (&tt));
else
fprintf (stream, "Umounts/dev/fd0 %s", ctime (&tt));

fclose(stream);
}
sleep(1);
}

У вас floppy_check() вызывается в бесконечном цыкле.
Vladivostok Linux User Group
Спасибо сказали: