본문 바로가기
Raspberry Pi (Linux, ubuntu)

Shutdown and Reboot from inside a C program

by 소나무기운 2022. 9. 12.
반응형

[2022/09/11] First Start.

 

소나무 기운 ,  전자제품 개발/생산

Shutdown and Reboot from inside a C program

@ Raspberry pi vertion

pi@raspberrypi:~ $ cat /etc/debian_version 
11.2
pi@raspberrypi:~ $ cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 11 (bullseye)"
NAME="Raspbian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 5.10.103-v7l+ #1530 SMP Tue Mar 8 13:05:01 GMT 2022 armv7l GNU/Linux
pi@raspberrypi:~ $

@ command example

 

system("sudo reboot");

system("sudo shutdown now");

 

@ example code

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
	system("sudo reboot");
	return 0;
}

 

System() Function in C/C++

Given the task is to show the working of system() in C/C++.The system() function is a port of the C/C++ standard library. It is used to pass the commands that can be executed in the command processsor or the terminal of the operating system, and finally returns the command after it has been completed.<stdlib.h> or <cstdlib> shuld be included to call this function.

 

Syntax

The systax is as follows -

int system(char command)

This fuction returns zero if the command is executed without any errors.

 

 

마무리

#include <iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int main() {
   char cmd[10];
   strcpy(cmd,"dir");
   system(cmd);
   return0;
}
#include <iostream>
#include<stdlib.h>
using namespace std;
int main() {
   if(system(Null))
      cout<<”Command Processor is present”;
   else
      cout<<”Command processor is not present”;
   return 0;
}

참고문헌

 
 

 

 

틀린 부분이나 질문은 댓글 달아주세요.

즐거운 하루 보내세요. 감사합니다.

 

 

반응형

댓글